:root {
  --bg: #f6f5f2;
  --card: #ffffff;
  --text: #1d1d1b;
  --muted: #6b6a66;
  --line: #dddad3;
  --accent: #b5452f;
  --accent-soft: #fbeee9;
  --sel: #1f6feb;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; font: 15px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif; background: var(--bg); color: var(--text);
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
h1 { font-size: 20px; margin: 0; }
h2 { font-size: 13px; margin: 0 0 8px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); display: inline; }
.top { padding: 14px 16px 8px; }
.sub { margin: 4px 0 0; color: var(--muted); font-size: 14px; }
.muted { color: var(--muted); font-size: 13px; }
.warn { color: #ffb4a3; }
button, .btn {
  font: inherit; font-size: 15px; padding: 9px 14px; min-height: 40px; border-radius: 8px; border: 1px solid var(--accent);
  background: var(--accent); color: #fff; cursor: pointer; text-decoration: none; display: inline-flex; align-items: center; justify-content: center;
  -webkit-tap-highlight-color: transparent; touch-action: manipulation;
}
button.secondary, .btn.secondary { background: transparent; color: var(--accent); }
button.small { min-height: 34px; padding: 6px 10px; font-size: 14px; }
button.big { font-size: 17px; padding: 12px 20px; min-height: 48px; }
.btn.disabled { opacity: .45; pointer-events: none; }
input[type=number], input[type=text], select {
  font: inherit; font-size: 16px; /* 16px stops iOS from zooming in on focus */
  padding: 6px 8px; border: 1px solid var(--line); border-radius: 6px; width: 100%; background: #fff; color: inherit; min-height: 38px;
}
label { display: block; margin: 6px 0; font-size: 14px; }
label.check { display: flex; gap: 8px; align-items: center; }
label.check input { width: 18px; height: 18px; margin: 0; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-top: 8px; }
.row select { width: auto; flex: 1; min-width: 150px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 10px; }

.drop { margin: 16px; padding: 40px 16px; border: 2px dashed var(--line); border-radius: 12px; text-align: center; background: var(--card); }
.drop.over { border-color: var(--accent); background: var(--accent-soft); }
@media (hover: none) { .desktop-only { display: none; } }

main { display: grid; grid-template-columns: 340px 1fr; gap: 16px; padding: 0 16px 16px; height: calc(100vh - 80px); height: calc(100dvh - 80px); }
.side { overflow: auto; padding-right: 4px; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; margin-bottom: 12px; }
.card.accent { border-color: var(--accent); background: var(--accent-soft); }
details.card summary { cursor: pointer; list-style: revert; }
details.card[open] summary { margin-bottom: 6px; }
.card > h2 { display: block; }
.notes { margin: 8px 0; padding-left: 18px; font-size: 13px; }
.notes li.warn { color: var(--accent); }

.preview { display: flex; flex-direction: column; min-height: 0; background: var(--card); border: 1px solid var(--line); border-radius: 10px; }
.toolbar { display: flex; gap: 14px; align-items: center; padding: 8px 12px; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.toolbar label { margin: 0; display: flex; gap: 6px; align-items: center; }
.toolbar input[type=range] { width: 120px; }
.scroller { flex: 1; overflow: auto; padding: 12px; background: #ebe9e4; -webkit-overflow-scrolling: touch; }
.board { position: relative; margin: 0 auto; touch-action: manipulation; }
.cell { position: absolute; background-color: #fff; background-repeat: no-repeat; cursor: pointer; }
.cell.empty { background: transparent; outline: 1px dashed #c4c0b7; outline-offset: -1px; }
.board.seams .cell:not(.empty) { box-shadow: inset 0 0 0 0.5px rgba(181, 69, 47, .55); }
.cell .num { position: absolute; left: 2px; top: 2px; font-size: 10px; line-height: 1.3; background: rgba(29, 29, 27, .75); color: #fff; padding: 0 3px; border-radius: 3px; pointer-events: none; }
.board.nonums .num { display: none; }
.cell.sel { outline: 3px solid var(--sel); outline-offset: -3px; z-index: 2; }

/* Phones and small windows: one column. Landscape phones are wide but not tall. */
@media (max-width: 900px), (max-height: 500px) {
  main { display: flex; flex-direction: column-reverse; height: auto; padding: 0 10px 16px; gap: 10px; }
  .side { overflow: visible; padding: 0; }
  .preview { height: 58vh; height: 58dvh; min-height: 260px; }
  .top { padding: 10px 12px 6px; }
  .sub { display: none; }
}
@media (max-height: 500px) and (min-width: 600px) {
  /* Landscape phone: two short columns of cards save scrolling. */
  .side { display: grid; grid-template-columns: 1fr 1fr; gap: 0 10px; align-items: start; }
}

/* Projector mode */
body.projecting { position: fixed; inset: 0; overflow: hidden; }
#proj { position: fixed; inset: 0; background: #000; z-index: 100; overflow: hidden; color: #eee; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; touch-action: none; }
#projCanvas, #overlay { position: absolute; left: 0; top: 0; width: 100%; height: 100%; }
#overlay { pointer-events: none; }
#handles .h { position: absolute; width: 44px; height: 44px; margin: -22px 0 0 -22px; border-radius: 50%; border: 2px solid #ffd21f; cursor: move; touch-action: none; }
#handles .h::after { content: attr(data-n); position: absolute; left: 44px; top: -16px; color: #ffd21f; font: bold 16px system-ui; }
#handles .h.active { background: rgba(255, 210, 31, .22); }
#handles .h::before { content: ""; position: absolute; left: 50%; top: 50%; width: 1px; height: 1px; box-shadow: 0 0 0 1px #ffd21f; }
#loading { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); font-size: 18px; background: rgba(0, 0, 0, .7); padding: 8px 14px; border-radius: 8px; }
.gear { position: absolute; left: calc(8px + env(safe-area-inset-left)); top: calc(8px + env(safe-area-inset-top)); width: 44px; height: 44px; min-height: 0; padding: 0; border-radius: 50%; background: rgba(40, 40, 40, .6); border-color: #555; color: #bbb; font-size: 22px; opacity: .5; }
#panel {
  position: absolute; right: calc(12px + env(safe-area-inset-right)); top: calc(12px + env(safe-area-inset-top));
  width: min(380px, calc(100vw - 24px)); max-height: calc(100% - 24px); overflow: auto; -webkit-overflow-scrolling: touch; touch-action: pan-y;
  background: rgba(28, 28, 28, .95); border: 1px solid #444; border-radius: 12px; padding: 10px 12px; font-size: 14px;
}
#panel.hidden { display: none; }
#panel p { margin: 6px 0; }
#panel .muted { color: #aaa; }
#panel label { font-size: 14px; }
#panel input[type=number], #panel select { background: #111; color: #eee; border-color: #555; }
#panel .tabs { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 6px; }
#panel .tabs button { padding: 6px 10px; min-height: 36px; font-size: 14px; flex: 1 1 auto; }
#panel button.secondary, #panel .tabs button[data-mode] { background: transparent; color: #f0b3a5; border-color: #8a4a3b; }
#panel .tabs button[data-mode].on { background: var(--accent); color: #fff; border-color: var(--accent); }
.pad { display: flex; gap: 10px; align-items: center; justify-content: space-between; border: 1px solid #3a3a3a; border-radius: 10px; padding: 8px; margin: 6px 0 8px; }
.pad-left { flex: 1; min-width: 0; }
.pad-left label { margin: 0 0 6px; }
.pad .corners { display: grid; grid-template-columns: 44px 44px; gap: 6px; margin-bottom: 6px; }
.pad .corners button { min-height: 38px; padding: 0; background: transparent; color: #ffd21f; border-color: #6b5a12; }
.pad .corners button.on { background: #ffd21f; color: #000; }
.pad .arrows { display: grid; grid-template-columns: repeat(3, 44px); grid-template-rows: repeat(3, 40px); gap: 4px; flex: none; }
.pad .arrows button { min-height: 0; padding: 0; background: #333; border-color: #555; color: #eee; font-size: 17px; }
.pad .arrows [data-dir="0,-1"] { grid-column: 2; grid-row: 1; }
.pad .arrows [data-dir="-1,0"] { grid-column: 1; grid-row: 2; }
.pad .arrows [data-dir="1,0"] { grid-column: 3; grid-row: 2; }
.pad .arrows [data-dir="0,1"] { grid-column: 2; grid-row: 3; }
#panel details summary { cursor: pointer; color: #ccc; margin: 4px 0; }
#panel.flip { right: auto; left: calc(12px + env(safe-area-inset-left)); }

/* Portrait phone: the panel is a bottom sheet. */
@media (max-width: 600px) and (orientation: portrait) {
  #panel { left: 0; right: 0; top: auto; bottom: 0; width: auto; max-height: 50%; border-radius: 14px 14px 0 0; padding-bottom: calc(10px + env(safe-area-inset-bottom)); }
  #panel.flip { left: 0; top: 0; bottom: auto; border-radius: 0 0 14px 14px; padding-top: calc(10px + env(safe-area-inset-top)); padding-bottom: 10px; }
}
/* Landscape phone: a narrow panel on the right, full height, arrows beside the options. */
@media (max-height: 500px) and (orientation: landscape) {
  #panel { top: 0; right: 0; bottom: 0; max-height: none; width: min(330px, 44vw); border-radius: 12px 0 0 12px; padding-right: calc(10px + env(safe-area-inset-right)); font-size: 13px; }
  #panel.flip { left: 0; right: auto; border-radius: 0 12px 12px 0; padding-left: calc(10px + env(safe-area-inset-left)); padding-right: 12px; }
  .pad .arrows { grid-template-rows: repeat(3, 36px); }
}
