/* Solarized light and dark. "Auto" follows the system; the settings can
   pin either one through data-theme on the root element. Sizes are in
   rem, em and percent; touch targets are at least 2.75rem. */

:root {
    --base03: #002b36;
    --base02: #073642;
    --base01: #586e75;
    --base00: #657b83;
    --base0: #839496;
    --base1: #93a1a1;
    --base2: #eee8d5;
    --base3: #fdf6e3;
    --yellow: #b58900;
    --orange: #cb4b16;
    --red: #dc322f;
    --magenta: #d33682;
    --violet: #6c71c4;
    --blue: #268bd2;
    --cyan: #2aa198;
    --green: #859900;

    --bg: var(--base3);
    --bg-alt: var(--base2);
    --fg: var(--base00);
    --fg-em: var(--base01);
    --line: rgba(88, 110, 117, 0.28);
    --sel-bg: rgba(203, 75, 22, 0.14);
    --rel-bg: rgba(203, 75, 22, 0.06);
    --shadow: rgba(0, 43, 54, 0.18);

    /* Board drawing (read by the canvas). */
    --c-bg: var(--base3);
    --c-board: var(--base2);
    --c-edge: var(--base01);
    --c-part: var(--base3);
    --c-part-edge: var(--base00);
    --c-pad: var(--yellow);
    --c-pad1: var(--magenta);
    --c-symbol: var(--blue);
    --c-ref: var(--base01);
    --c-value: var(--violet);
    --c-hl: var(--orange);
    --c-focus: var(--red);
    --c-search: var(--green);
    --c-placed: var(--green);
    --c-dnp: var(--red);
    --c-label-bg: var(--base3);

    color-scheme: light;
}

@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        --bg: var(--base03);
        --bg-alt: var(--base02);
        --fg: var(--base0);
        --fg-em: var(--base1);
        --line: rgba(147, 161, 161, 0.22);
        --sel-bg: rgba(203, 75, 22, 0.22);
        --rel-bg: rgba(203, 75, 22, 0.1);
        --shadow: rgba(0, 0, 0, 0.45);
        --c-bg: var(--base03);
        --c-board: var(--base02);
        --c-edge: var(--base1);
        --c-part: var(--base03);
        --c-part-edge: var(--base0);
        --c-ref: var(--base1);
        --c-value: var(--cyan);
        --c-label-bg: var(--base03);
        color-scheme: dark;
    }
}

:root[data-theme="dark"] {
    --bg: var(--base03);
    --bg-alt: var(--base02);
    --fg: var(--base0);
    --fg-em: var(--base1);
    --line: rgba(147, 161, 161, 0.22);
    --sel-bg: rgba(203, 75, 22, 0.22);
    --rel-bg: rgba(203, 75, 22, 0.1);
    --shadow: rgba(0, 0, 0, 0.45);
    --c-bg: var(--base03);
    --c-board: var(--base02);
    --c-edge: var(--base1);
    --c-part: var(--base03);
    --c-part-edge: var(--base0);
    --c-ref: var(--base1);
    --c-value: var(--cyan);
    --c-label-bg: var(--base03);
    color-scheme: dark;
}

* {
    box-sizing: border-box;
}

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

button,
input {
    font: inherit;
}

/* No double-tap zoom delay on controls. */
button,
label,
.group,
.file {
    touch-action: manipulation;
}

a {
    color: var(--blue);
}

.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
}

.icon {
    width: 1.4em;
    height: 1.4em;
    fill: none;
    stroke: currentColor;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
    flex: none;
}

/* --- Layout ----------------------------------------------------------- */

.screen {
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
}

.topbar {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    padding: calc(0.4rem + env(safe-area-inset-top, 0px)) max(0.5rem, env(safe-area-inset-right, 0px)) 0.4rem
        max(0.5rem, env(safe-area-inset-left, 0px));
    background: var(--bg-alt);
    border-bottom: 1px solid var(--line);
    flex: none;
}

.topbar-title {
    flex: 1;
    min-width: 0;
    font-weight: 600;
    color: var(--fg-em);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.page {
    flex: 1;
    overflow-y: auto;
    width: 100%;
    max-width: 42rem;
    margin: 0 auto;
    padding: 1.25rem max(1rem, env(safe-area-inset-right, 0px)) calc(2rem + env(safe-area-inset-bottom, 0px))
        max(1rem, env(safe-area-inset-left, 0px));
}

/* --- Controls --------------------------------------------------------- */

.icon-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 2.75rem;
    height: 2.75rem;
    padding: 0;
    border: none;
    border-radius: 0.7rem;
    background: transparent;
    color: var(--fg-em);
    cursor: pointer;
    flex: none;
}

.icon-btn:hover {
    background: var(--line);
}

.icon-btn.small {
    width: 2.25rem;
    height: 2.25rem;
}

.icon-btn.float {
    position: absolute;
    top: 0.75rem;
    right: 0.75rem;
    background: var(--bg-alt);
    border: 1px solid var(--line);
    box-shadow: 0 0.15rem 0.6rem var(--shadow);
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    min-height: 2.75rem;
    padding: 0.6rem 1.1rem;
    border-radius: 0.7rem;
    border: 1px solid var(--line);
    background: var(--bg-alt);
    color: var(--fg-em);
    font-weight: 600;
    cursor: pointer;
}

.btn.primary {
    background: var(--blue);
    border-color: var(--blue);
    color: var(--base3);
}

.btn.danger {
    background: transparent;
    border-color: var(--red);
    color: var(--red);
}

.chip {
    border: 1px solid var(--line);
    border-radius: 999px;
    padding: 0.3rem 0.6rem;
    background: var(--bg);
    color: var(--fg-em);
    font-size: 0.8rem;
    white-space: nowrap;
    cursor: pointer;
}

:focus-visible {
    outline: 0.15rem solid var(--blue);
    outline-offset: 0.1rem;
}

.search {
    flex: 1;
    min-width: 0;
    display: flex;
    align-items: center;
    height: 2.6rem;
    padding: 0 0.15rem 0 0.7rem;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: var(--bg);
    color: var(--fg);
}

.search-icon {
    display: inline-flex;
    color: var(--fg);
}

.search-input {
    flex: 1;
    min-width: 0;
    height: 100%;
    padding: 0 0.4rem;
    border: none;
    background: transparent;
    color: var(--fg-em);
    /* 16px or more keeps iOS from zooming in on focus. */
    font-size: 1rem;
    outline: none;
}

.search-input::-webkit-search-cancel-button {
    display: none;
}

.clear-search {
    width: 2.25rem;
    height: 2.25rem;
}

.side-btn {
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
    height: 2.6rem;
    padding: 0 0.75rem;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: var(--bg);
    color: var(--fg-em);
    font-weight: 600;
    cursor: pointer;
    flex: none;
}

@media (max-width: 24rem) {
    .side-btn span {
        display: none;
    }
    .side-btn {
        padding: 0 0.6rem;
    }
}

/* --- Board and parts list ----------------------------------------------- */

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

.board-canvas {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    display: block;
    touch-action: none;
    cursor: grab;
}

.sheet {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    display: flex;
    flex-direction: column;
    max-height: 92%;
    background: var(--bg-alt);
    border-top-left-radius: 1rem;
    border-top-right-radius: 1rem;
    box-shadow: 0 -0.2rem 1rem var(--shadow);
}

.sheet.collapsed {
    height: auto;
    padding-bottom: env(safe-area-inset-bottom, 0px);
}

.sheet.collapsed .sheet-body {
    display: none;
}

.sheet-head {
    flex: none;
    padding: 0.4rem max(0.75rem, env(safe-area-inset-right, 0px)) 0.45rem max(0.9rem, env(safe-area-inset-left, 0px));
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    cursor: ns-resize;
}

.grip {
    width: 2.75rem;
    height: 0.3rem;
    margin: 0 auto 0.35rem;
    border-radius: 1rem;
    background: var(--base1);
    opacity: 0.8;
}

.sheet-row {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    min-height: 2.75rem;
}

.summary {
    flex: 1;
    min-width: 0;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.sum-text {
    flex: 1;
    min-width: 0;
}

.sum-main,
.sum-sub {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.sum-main {
    color: var(--fg-em);
    font-size: 1rem;
}

.sum-sub {
    font-size: 0.85rem;
}

.sheet-body {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    border-top: 1px solid var(--line);
    padding-bottom: env(safe-area-inset-bottom, 0px);
}

.groups {
    list-style: none;
    margin: 0;
    padding: 0 0 1rem;
}

.groups .section {
    padding: 0.9rem 1rem 0.3rem;
    font-size: 0.75rem;
    font-weight: 700;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--fg);
}

.groups .empty {
    padding: 1.25rem 1rem;
}

.group {
    display: grid;
    grid-template-columns: 2.6rem minmax(0, 1fr) auto;
    gap: 0.6rem;
    align-items: start;
    padding: 0.55rem 0.4rem 0.55rem 0.8rem;
    border-left: 0.25rem solid transparent;
    cursor: pointer;
}

.group:hover {
    background: var(--rel-bg);
}

.group.selected {
    background: var(--sel-bg);
    border-left-color: var(--orange);
}

.group.related {
    background: var(--rel-bg);
    border-left-color: var(--orange);
}

.sym-wrap {
    display: flex;
    align-items: center;
    height: 1.6rem;
}

.sym {
    width: 2.6rem;
    height: 1.4rem;
    fill: none;
    stroke: var(--blue);
    stroke-width: 1.6px;
    stroke-linecap: round;
    stroke-linejoin: round;
    vector-effect: non-scaling-stroke;
}

.sym path {
    vector-effect: non-scaling-stroke;
}

.g-line {
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    gap: 0.15rem 0.5rem;
}

.g-value {
    font-weight: 700;
    font-size: 1.05rem;
    color: var(--fg-em);
    overflow-wrap: anywhere;
}

.group.dnp .g-value {
    color: var(--red);
}

.group.done .g-value {
    text-decoration: line-through;
    text-decoration-color: var(--green);
    text-decoration-thickness: 0.12em;
}

.g-count {
    font-size: 0.8rem;
    padding: 0.05rem 0.5rem;
    border-radius: 999px;
    border: 1px solid var(--line);
    background: var(--bg);
    color: var(--fg-em);
}

.g-pkg {
    font-size: 0.8rem;
    overflow-wrap: anywhere;
}

.refs {
    margin-top: 0.2rem;
    font-size: 0.85rem;
    line-height: 1.5;
    overflow-wrap: anywhere;
}

.ref.hit {
    color: var(--green);
    font-weight: 700;
}

.ref.on {
    text-decoration: line-through;
    opacity: 0.6;
}

.ref.away {
    font-style: italic;
    opacity: 0.65;
}

.ref-chip {
    min-height: 2.25rem;
    margin: 0.15rem 0.3rem 0.15rem 0;
    padding: 0.25rem 0.65rem;
    border: 1px solid var(--line);
    border-radius: 0.5rem;
    background: var(--bg);
    color: var(--fg-em);
    font-size: 0.9rem;
    cursor: pointer;
}

.ref-chip.on {
    background: var(--green);
    border-color: var(--green);
    color: var(--base3);
}

.ref-chip.away {
    border-style: dashed;
}

.ref-chip.focus {
    outline: 0.13rem solid var(--red);
}

.g-hint {
    margin-top: 0.25rem;
    font-size: 0.75rem;
}

.check-wrap {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 2.75rem;
    height: 2.75rem;
    cursor: pointer;
}

.check-wrap input {
    width: 1.35rem;
    height: 1.35rem;
    margin: 0;
    accent-color: var(--green);
    cursor: pointer;
}

/* --- Home, listing, status ----------------------------------------------- */

.home-head .logo {
    display: inline-flex;
    color: var(--blue);
    font-size: 2rem;
}

.home-head h1 {
    margin: 0.2rem 0 0.4rem;
    font-size: 1.8rem;
    color: var(--fg-em);
}

.home-head p {
    margin: 0 0 1.5rem;
    line-height: 1.5;
}

.open-form label {
    display: block;
    margin-bottom: 0.4rem;
    font-weight: 600;
    color: var(--fg-em);
}

.open-form .row {
    display: flex;
    gap: 0.5rem;
}

.open-form input {
    flex: 1;
    min-width: 0;
    min-height: 2.75rem;
    padding: 0.6rem 0.9rem;
    border: 1px solid var(--line);
    border-radius: 0.7rem;
    background: var(--bg-alt);
    color: var(--fg-em);
    font-size: 1rem;
}

.hint,
.note {
    font-size: 0.85rem;
    line-height: 1.45;
}

.error {
    color: var(--red);
    font-weight: 600;
    line-height: 1.45;
}

.file-btn {
    margin-top: 0.5rem;
}

.recents h2,
.listing h2 {
    margin: 1.75rem 0 0.5rem;
    font-size: 1rem;
    color: var(--fg-em);
}

.files {
    list-style: none;
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    gap: 0.45rem;
}

.file {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    min-height: 3.25rem;
    padding: 0.7rem 0.9rem;
    border: 1px solid var(--line);
    border-radius: 0.8rem;
    background: var(--bg-alt);
    color: inherit;
    text-decoration: none;
}

.file:hover {
    border-color: var(--blue);
}

.file-icon {
    display: inline-flex;
    color: var(--blue);
}

.file-text {
    min-width: 0;
}

.file-name {
    display: block;
    font-weight: 600;
    color: var(--fg-em);
    overflow-wrap: anywhere;
}

.file-detail {
    display: block;
    font-size: 0.8rem;
    overflow-wrap: anywhere;
}

.status {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 1rem;
    text-align: center;
}

.spinner {
    width: 2.5rem;
    height: 2.5rem;
    border-radius: 50%;
    border: 0.25rem solid var(--line);
    border-top-color: var(--blue);
    animation: spin 0.9s linear infinite;
}

@keyframes spin {
    to {
        transform: rotate(1turn);
    }
}

/* --- Settings ----------------------------------------------------------- */

dialog.settings {
    width: min(30rem, calc(100% - 2rem));
    max-height: calc(100dvh - 2rem);
    padding: 0;
    border: none;
    border-radius: 1rem;
    background: var(--bg);
    color: var(--fg);
    box-shadow: 0 0.5rem 2rem var(--shadow);
}

dialog.settings::backdrop {
    background: rgba(0, 43, 54, 0.45);
}

dialog.settings form {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    padding: 1.25rem;
}

dialog.settings h2 {
    margin: 0;
    font-size: 1.2rem;
    color: var(--fg-em);
}

.setting {
    display: flex;
    gap: 0.75rem;
    align-items: flex-start;
    cursor: pointer;
}

.setting input {
    width: 1.35rem;
    height: 1.35rem;
    margin: 0.1rem 0 0;
    accent-color: var(--blue);
    flex: none;
}

.setting span {
    display: flex;
    flex-direction: column;
    font-weight: 600;
    color: var(--fg-em);
}

.setting small {
    margin-top: 0.15rem;
    font-size: 0.85rem;
    font-weight: 400;
    color: var(--fg);
}

.themes {
    display: flex;
    gap: 0.5rem;
    margin: 0;
    padding: 0;
    border: none;
}

.themes legend {
    margin-bottom: 0.45rem;
    font-weight: 600;
    color: var(--fg-em);
}

.seg {
    flex: 1;
    position: relative;
}

.seg input {
    position: absolute;
    opacity: 0;
    inset: 0;
    margin: 0;
}

.seg span {
    display: block;
    padding: 0.6rem 0.4rem;
    border: 1px solid var(--line);
    border-radius: 0.6rem;
    text-align: center;
    color: var(--fg-em);
    cursor: pointer;
}

.seg input:checked + span {
    background: var(--blue);
    border-color: var(--blue);
    color: var(--base3);
}

.seg input:focus-visible + span {
    outline: 0.15rem solid var(--blue);
    outline-offset: 0.1rem;
}

.source {
    margin: 0;
    font-size: 0.9rem;
}

.dialog-actions {
    display: flex;
    justify-content: flex-end;
}
