:root {
    --bg: #14201a;
    --panel: #1e2d24;
    --panel-2: #26382d;
    --line: #3a4d40;
    --text: #f2efe4;
    --muted: #c9c3b0;
    --accent: #f5c542;
    --accent-ink: #1a1608;
    --danger: #ff8a7a;
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    height: 100%;
    overflow: hidden;
    background: var(--bg);
    color: var(--text);
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    -webkit-tap-highlight-color: transparent;
    overscroll-behavior: none;
}

body {
    display: flex;
    flex-direction: column;
    height: 100dvh;
}

svg {
    width: 22px;
    height: 22px;
    fill: none;
    stroke: currentColor;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
}

/* ---------- buttons ---------- */

.btn {
    font: inherit;
    font-size: 15px;
    font-weight: 600;
    min-height: 44px;
    min-width: 44px;
    padding: 0 14px;
    border-radius: 10px;
    border: 0;
    color: var(--text);
    background: var(--panel-2);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    cursor: pointer;
    text-decoration: none;
}

.btn.ghost { background: transparent; padding: 0 10px; }
.btn.ghost:hover { background: var(--panel-2); }
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn:disabled { background: var(--panel-2); color: #aaa696; cursor: default; }
.btn.ghost:disabled { background: transparent; }
.btn:focus-visible, .tool:focus-visible, select:focus-visible, input:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

.btn.round {
    border-radius: 50%;
    padding: 0;
    background: rgba(30, 45, 36, 0.92);
    border: 1px solid var(--line);
}

.spacer { flex: 1; }

/* ---------- top bar ---------- */

.bar {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 6px 8px;
    padding-top: max(6px, env(safe-area-inset-top));
    background: var(--panel);
    border-bottom: 1px solid var(--line);
}

select, input {
    font: inherit;
    font-size: 16px;
    color: var(--text);
    background: var(--bg);
    border: 1px solid var(--line);
    border-radius: 10px;
    min-height: 44px;
    padding: 0 10px;
}

#map-select { flex: 1; min-width: 0; max-width: 240px; margin-right: auto; }

/* narrow phones: save button only shows the disk icon */
@media (max-width: 420px) {
    #btn-save .label { display: none; }
    #btn-save { padding: 0 12px; }
}

/* ---------- canvas ---------- */

#stage {
    position: relative;
    flex: 1;
    min-height: 0;
    overflow: hidden;
}

#canvas {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    touch-action: none;
    cursor: crosshair;
}

#canvas.panning { cursor: grab; }

#status {
    position: absolute;
    left: 8px;
    bottom: 8px;
    max-width: calc(100% - 80px);
    padding: 6px 10px;
    border-radius: 8px;
    font-size: 13px;
    background: rgba(20, 32, 26, 0.88);
    color: var(--muted);
    pointer-events: none;
}

#status:empty { display: none; }
#status.error { color: var(--danger); }

.history {
    position: absolute;
    left: 8px;
    top: 8px;
    display: flex;
    gap: 8px;
}

.btn.round:disabled { opacity: 0.45; background: rgba(30, 45, 36, 0.92); }

.zoom {
    position: absolute;
    right: 8px;
    bottom: 8px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

/* ---------- tools ---------- */

.tools {
    background: var(--panel);
    border-top: 1px solid var(--line);
    padding-bottom: env(safe-area-inset-bottom);
}

.toolrow, .items {
    display: flex;
    gap: 4px;
    padding: 6px 8px;
    overflow-x: auto;
    scrollbar-width: none;
}

.toolrow::-webkit-scrollbar, .items::-webkit-scrollbar { display: none; }

.items {
    border-bottom: 1px solid var(--line);
}

.items[hidden] { display: none; }

.tool {
    flex: 1 0 56px;
    max-width: 76px;
    min-height: 56px;
    padding: 6px 2px 4px;
    border: 2px solid transparent;
    border-radius: 10px;
    background: transparent;
    color: var(--text);
    font: inherit;
    font-size: 11px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 4px;
    cursor: pointer;
}

.tool img {
    width: 24px;
    height: 24px;
    image-rendering: pixelated;
    border-radius: 4px;
    object-fit: contain;
}

.tool:hover { background: var(--panel-2); }
.tool[aria-pressed="true"] { border-color: var(--accent); background: var(--panel-2); }
.tool:disabled { opacity: 0.4; cursor: default; background: transparent; }

.items .tool { flex: 0 0 56px; }

/* ---------- dialogs ---------- */

dialog {
    width: min(420px, calc(100vw - 32px));
    border: 1px solid var(--line);
    border-radius: 14px;
    padding: 20px;
    background: var(--panel);
    color: var(--text);
}

dialog::backdrop { background: rgba(0, 0, 0, 0.55); }

dialog h2 { margin: 0 0 8px; font-size: 20px; }

dialog form { display: flex; flex-direction: column; gap: 12px; }

dialog label {
    display: flex;
    flex-direction: column;
    gap: 6px;
    font-size: 14px;
    font-weight: 600;
    color: var(--muted);
    flex: 1;
}

dialog .row { display: flex; gap: 12px; }
dialog .row input { width: 100%; }

.actions { display: flex; gap: 8px; align-items: center; margin-top: 4px; }

#portal-remove { color: var(--danger); }
.btn.danger { color: var(--danger); }

/* the staff of a map: one row per person */
#dlg-size { max-height: calc(100dvh - 32px); overflow-y: auto; }
.crew { display: flex; flex-direction: column; gap: 8px; }
.crew[hidden] { display: none; }
.crew-list { display: flex; flex-direction: column; gap: 6px; }
.crew-row {
    display: grid;
    grid-template-columns: 52px 1fr 36px;
    grid-template-areas: "face name del" "face role del" "pick pick pick";
    gap: 6px;
    align-items: center;
    padding: 6px;
    border-radius: 10px;
    background: var(--bg);
}
.crew-face {
    grid-area: face;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1px;
    padding: 2px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--panel);
    color: var(--muted);
    font: inherit;
    font-size: 10px;
    cursor: pointer;
}
.crew-face:hover, .crew-face[aria-expanded="true"] { border-color: var(--accent); color: var(--accent); }
.crew-face img { width: 40px; height: 40px; object-fit: contain; image-rendering: pixelated; }
.crew-row .crew-name { grid-area: name; }
.crew-row .crew-role { grid-area: role; }
.crew-pick {
    grid-area: pick;
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(40px, 1fr));
    gap: 4px;
    padding-top: 4px;
}
.crew-pick[hidden] { display: none; }
.crew-pick button {
    aspect-ratio: 1;
    padding: 2px;
    border: 2px solid transparent;
    border-radius: 8px;
    background: var(--panel);
    cursor: pointer;
}
.crew-pick button[aria-checked="true"] { border-color: var(--accent); }
.crew-pick button:hover { border-color: var(--line); }
.crew-pick img { width: 100%; height: 100%; object-fit: contain; image-rendering: pixelated; }
.crew-row .crew-del { grid-area: del; min-width: 36px; min-height: 36px; padding: 0; color: var(--danger); }
.crew-row input, .crew-row select { width: 100%; min-width: 0; min-height: 36px; font-size: 14px; }
.crew-empty { margin: 0; font-size: 13px; color: var(--muted); }
#crew-add { align-self: flex-start; min-height: 36px; }

dialog label.check { flex-direction: row; align-items: flex-start; gap: 10px; font-weight: 400; color: var(--text); cursor: pointer; }
dialog label.check input { width: 20px; height: 20px; margin: 2px 0 0; flex: 0 0 auto; accent-color: var(--accent); }
dialog label.check span { display: flex; flex-direction: column; gap: 2px; }
dialog label.check small { color: var(--muted); font-size: 12px; }
.btn.danger-fill { background: #c9443a; color: #fff; }
.btn.danger-fill:disabled { opacity: 0.6; }

.muted { color: var(--muted); margin: 0; }
.small { font-size: 13px; }

/* ---------- objects tray ---------- */

.tray {
    border-bottom: 1px solid var(--line);
}

.tray[hidden] { display: none; }

.tray-tabs[hidden] { display: none; }

.tray-tabs {
    display: flex;
    gap: 6px;
    padding: 8px 8px 0;
}

.tray-tab {
    min-height: 34px;
    padding: 0 12px;
    border-radius: 999px;
    border: 1px solid var(--line);
    background: transparent;
    color: var(--muted);
    font: inherit;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
}

.tray-tab[aria-selected="true"] {
    background: var(--accent);
    border-color: var(--accent);
    color: var(--accent-ink);
}

.items { border-bottom: 0; }

.tool-icon {
    width: 24px;
    height: 24px;
    font-size: 21px;
    line-height: 24px;
    text-align: center;
}

.items .tool span:last-child {
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.who img {
    width: 28px;
    height: 28px;
    image-rendering: pixelated;
    object-fit: contain;
}

.error { color: var(--danger); margin: 0; min-height: 1.2em; }

dialog input { width: 100%; }

/* ---------- portals ---------- */

.portal-now {
    margin: 0;
    padding: 8px 10px;
    border-radius: 10px;
    background: var(--panel-2);
    font-size: 14px;
}

.seg {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin: 0;
    padding: 0;
    border: 0;
}

.seg label {
    flex-direction: row;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    border: 2px solid var(--line);
    border-radius: 10px;
    cursor: pointer;
    color: var(--text);
}

.seg label:has(input:checked) { border-color: var(--accent); background: var(--panel-2); }
.seg input { width: auto; min-height: 0; accent-color: var(--accent); }
.seg span { display: flex; flex-direction: column; gap: 2px; }
.seg small { font-weight: 400; color: var(--muted); }

#portal-remove { padding: 0 8px; }

/* uploaded files */
.upload { cursor: pointer; align-self: flex-start; flex: 0 0 auto; flex-direction: row; color: var(--text); font-size: 15px; }
.tpl-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(88px, 1fr));
    gap: 8px;
    max-height: 220px;
    overflow-y: auto;
}
.tpl-list:empty { display: none; }
.tpl-item {
    display: flex;
    flex-direction: column;
    gap: 4px;
    padding: 4px;
    border: 2px solid var(--line);
    border-radius: 10px;
    background: var(--bg);
    color: var(--muted);
    font: inherit;
    font-size: 11px;
    text-align: left;
}
.tpl-item img, .tpl-item .pdf {
    width: 100%;
    aspect-ratio: 4 / 3;
    object-fit: contain;
    background: #fff;
    border-radius: 6px;
}
.tpl-item .pdf { display: grid; place-items: center; color: #a33; font-weight: 700; font-size: 16px; }
.tpl-item span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* "tap where you come out" - shown on top of the target map */
.pickbar {
    position: absolute;
    left: 50%;
    top: 8px;
    transform: translateX(-50%);
    max-width: calc(100% - 140px);
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 4px 4px 4px 14px;
    border-radius: 12px;
    background: var(--accent);
    color: var(--accent-ink);
    font-size: 14px;
    font-weight: 600;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
}

.pickbar[hidden] { display: none; }
.pickbar .btn { color: var(--accent-ink); min-height: 36px; }
.pickbar .btn:hover { background: rgba(0, 0, 0, 0.12); }

@media (max-width: 520px) {
    .pickbar { top: 60px; max-width: calc(100% - 16px); }
}
