:root {
    --bg: #f6f5f0;
    --panel: #fffefb;
    --ink: #1d2327;
    --muted: #7b8186;
    --line: #d9d8d1;
    --cell-empty: #fffefb;
    --cell-border: #cfcec6;
    --cell-past: #2b3338;
    --accent: #e0703c;
    --field: #f1f0ea;
    --radius: 8px;
    color-scheme: light;
}

/* Dark follows the system unless the theme button picked one (data-theme on <html>) */
@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        --bg: #0b1a22;
        --panel: #0f222c;
        --ink: #e9ece8;
        --muted: #8a9aa2;
        --line: #23404d;
        --cell-empty: #0f222c;
        --cell-border: #2c4a57;
        --cell-past: #d9ddd6;
        --accent: #f08a55;
        --field: #142a35;
        color-scheme: dark;
    }
}
:root[data-theme="dark"] {
    --bg: #0b1a22;
    --panel: #0f222c;
    --ink: #e9ece8;
    --muted: #8a9aa2;
    --line: #23404d;
    --cell-empty: #0f222c;
    --cell-border: #2c4a57;
    --cell-past: #d9ddd6;
    --accent: #f08a55;
    --field: #142a35;
    color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    background: var(--bg);
    color: var(--ink);
    font-family: 'IBM Plex Serif', Georgia, serif;
    -webkit-font-smoothing: antialiased;
}

button, input { font: inherit; color: inherit; }
button { cursor: pointer; }

h1, h2, .btn, .segmented button, .add, .lang button, .stats, .axis, .legend, .tip strong {
    font-family: 'Unbounded', system-ui, sans-serif;
}

/* Layout: settings on the left, the poster on the right; stacked on narrow screens */
.app {
    display: grid;
    grid-template-columns: 380px 1fr;
    min-height: 100dvh;
    transition: grid-template-columns 0.35s ease;
}

.panel {
    position: sticky;
    top: 0;
    height: 100dvh;
    overflow-y: auto;
    background: var(--panel);
    border-right: 1px solid var(--line);
    overflow-x: hidden;
    padding: 28px 24px;
    display: flex;
    flex-direction: column;
    gap: 28px;
}
/* Fixed width, so the content slides out instead of squeezing while the panel folds */
.panel > * { width: 332px; flex-shrink: 0; }
.panel-body { flex: 1; display: flex; flex-direction: column; gap: 28px; }

.icon-btn {
    display: grid;
    place-items: center;
    width: 28px;
    height: 28px;
    padding: 0;
    border: 0;
    border-radius: 6px;
    background: none;
    color: var(--muted);
}
.icon-btn:hover { color: var(--ink); background: var(--field); }
.lang .icon-btn { margin-left: 4px; }
.lang .icon-btn + .icon-btn { margin-left: 0; }
/* The theme button shows the theme it switches to */
#theme-toggle .sun { display: none; }
[data-dark] #theme-toggle .sun { display: block; }
[data-dark] #theme-toggle .moon { display: none; }

/* Folded: the panel slides away and a button at the top left brings it back */
.panel-open { display: none; }
.app.collapsed { grid-template-columns: 0 1fr; }
.app.collapsed .panel { padding-inline: 0; border-right-width: 0; visibility: hidden; transition: visibility 0s 0.35s, padding 0.35s; }
.app.collapsed .panel-open {
    display: grid;
    position: fixed;
    top: 24px;
    left: 16px;
    z-index: 5;
    background: var(--panel);
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.12);
}
.app.collapsed .stage { padding-left: 64px; }

.panel-head { position: relative; }

.back {
    display: inline-block;
    font-size: 0.85rem;
    color: var(--muted);
    text-decoration: none;
    margin-bottom: 18px;
}
.back:hover { color: var(--ink); }

h1 {
    font-size: 1.6rem;
    font-weight: 400;
    margin: 0 0 6px;
    letter-spacing: -0.01em;
}

.tagline { margin: 0; color: var(--muted); font-style: italic; }

.lang {
    position: absolute;
    top: 0;
    right: 0;
    display: flex;
    align-items: center;
    gap: 2px;
}
.lang button {
    border: 0;
    background: none;
    font-size: 0.7rem;
    padding: 4px 6px;
    border-radius: 4px;
    color: var(--muted);
}
.lang button[aria-pressed="true"] { color: var(--ink); background: var(--field); }

.block { display: flex; flex-direction: column; gap: 14px; }

h2 {
    font-size: 0.8rem;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    margin: 0;
}

.hint { margin: -6px 0 0; font-size: 0.85rem; color: var(--muted); line-height: 1.45; }

.field-row { display: flex; gap: 12px; }

.field { display: flex; flex-direction: column; gap: 6px; flex: 1; min-width: 0; }
.field > span:first-child { font-size: 0.8rem; color: var(--muted); }
.field--short { flex: 0 0 104px; }

input[type="date"], input[type="number"], input[type="text"] {
    width: 100%;
    min-width: 0;
    background: var(--field);
    border: 1px solid transparent;
    border-radius: 6px;
    padding: 8px 10px;
    font-size: 0.95rem;
    outline: none;
}
input:focus { border-color: var(--accent); }

.with-suffix { position: relative; display: block; }
.with-suffix input { padding-right: 34px; }
.with-suffix em {
    position: absolute;
    right: 10px;
    top: 50%;
    transform: translateY(-50%);
    font-style: normal;
    font-size: 0.8rem;
    color: var(--muted);
    pointer-events: none;
}

.segmented {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    background: var(--field);
    border-radius: var(--radius);
    padding: 3px;
}
.segmented button {
    border: 0;
    background: none;
    padding: 8px 4px;
    border-radius: 6px;
    font-size: 0.78rem;
    color: var(--muted);
    transition: background 0.2s, color 0.2s;
}
.segmented button[aria-checked="true"] {
    background: var(--panel);
    color: var(--ink);
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12);
}

/* Chapters and moments */
.entries { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.entries:empty { display: none; }

.entry {
    display: grid;
    grid-template-columns: 22px 1fr 28px;
    gap: 6px 8px;
    align-items: center;
    padding: 10px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
}
.entry .dates { grid-column: 1 / 4; display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 6px; }
.entry .dates:has(> :only-child) { grid-template-columns: 1fr; }
.entry .dates span { color: var(--muted); font-size: 0.8rem; }
.entry input { padding: 6px 6px; font-size: 0.82rem; }
.entry input[type="text"] { background: none; padding-left: 2px; }
.entry input[type="text"]:focus { background: var(--field); padding-left: 8px; }

.swatch {
    width: 18px;
    height: 18px;
    border-radius: 4px;
    border: 0;
    padding: 0;
    background: var(--c);
}

/* Moment symbols: one SVG mask per shape, painted with the moment's colour */
[data-sym="ring"] { --shape: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 10 10'%3E%3Ccircle cx='5' cy='5' r='4.1' fill='none' stroke='black' stroke-width='1.4'/%3E%3C/svg%3E"); }
[data-sym="dot"] { --shape: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 10 10'%3E%3Ccircle cx='5' cy='5' r='3.6'/%3E%3C/svg%3E"); }
[data-sym="star"] { --shape: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 10 10'%3E%3Cpolygon points='5,0.3 6.4,3.4 9.8,3.7 7.2,6 8,9.4 5,7.6 2,9.4 2.8,6 0.2,3.7 3.6,3.4'/%3E%3C/svg%3E"); }
[data-sym="diamond"] { --shape: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 10 10'%3E%3Cpolygon points='5,0.3 9.7,5 5,9.7 0.3,5'/%3E%3C/svg%3E"); }
[data-sym="heart"] { --shape: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 10 10'%3E%3Cpath d='M5 9.3 L1.1 5.4 A2.45 2.45 0 0 1 5 2.2 A2.45 2.45 0 0 1 8.9 5.4 Z'/%3E%3C/svg%3E"); }

.sym {
    display: block;
    background: var(--c, currentColor);
    -webkit-mask: var(--shape) center / contain no-repeat;
    mask: var(--shape) center / contain no-repeat;
}
.swatch.sym { border-radius: 0; }

.entry .dates--moment { grid-template-columns: 1fr; }
.symbols { display: flex; gap: 2px; }
.sym-pick {
    width: 26px;
    height: 26px;
    border: 0;
    border-radius: 5px;
    background: none;
    color: var(--muted);
    display: grid;
    place-items: center;
    padding: 0;
}
.sym-pick .sym { width: 13px; height: 13px; }
.sym-pick:hover { background: var(--field); }
.sym-pick[aria-checked="true"] { background: var(--field); color: var(--ink); }

.remove {
    border: 0;
    background: none;
    color: var(--muted);
    font-size: 1.1rem;
    line-height: 1;
    padding: 4px;
    border-radius: 4px;
}
.remove:hover { color: var(--ink); background: var(--field); }

.add {
    align-self: flex-start;
    border: 1px dashed var(--line);
    background: none;
    border-radius: var(--radius);
    padding: 8px 12px;
    font-size: 0.75rem;
    color: var(--muted);
}
.add:hover { color: var(--ink); border-color: var(--muted); }

.panel-foot { margin-top: auto; display: flex; flex-direction: column; gap: 12px; }
.panel-foot .hint { margin: 0; font-size: 0.78rem; }

.btn {
    border: 0;
    background: var(--ink);
    color: var(--bg);
    border-radius: var(--radius);
    padding: 10px 14px;
    font-size: 0.8rem;
}
.btn:hover { opacity: 0.88; }

/* Poster */
.stage {
    padding: 28px 32px 56px;
    display: flex;
    justify-content: flex-start;
}

.poster { width: 100%; max-width: 820px; }

.poster-head { margin-bottom: 20px; }

/* Title on the left, the weeks / months / years switch on the right, right above the grid */
.poster-top {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 12px 24px;
    margin-bottom: 10px;
}
.poster-top .poster-title { margin: 0; }
.view-controls { flex: 0 1 300px; display: flex; flex-direction: column; gap: 10px; }

.size-control {
    display: flex;
    align-items: center;
    gap: 12px;
    font-family: 'Unbounded', system-ui, sans-serif;
    font-size: 0.7rem;
    color: var(--muted);
}
.size-control input { flex: 1; min-width: 0; accent-color: var(--ink); }

.poster-title {
    font-size: clamp(1.4rem, 3vw, 2.2rem);
    font-weight: 300;
    text-transform: none;
    letter-spacing: -0.01em;
    margin-bottom: 10px;
}

.stats { margin: 0 0 14px; font-size: 0.8rem; color: var(--muted); line-height: 1.6; }
.stats b { color: var(--ink); font-weight: 500; }

.progress { height: 3px; background: var(--line); border-radius: 2px; overflow: hidden; }
.progress span { display: block; height: 100%; width: 0; background: var(--accent); transition: width 0.8s ease; }

.empty { color: var(--muted); font-style: italic; }
.empty[hidden] { display: none; }

.grid-wrap { position: relative; }

.axis {
    margin: 0 0 6px var(--label-w, 28px);
    font-size: 0.65rem;
    color: var(--muted);
}

.grid { display: flex; flex-direction: column; gap: var(--gap, 2px); }
/* Fewer, bigger squares in months and years: keep the grid narrow so it stays a calendar, not a wall */
/* Week squares are tiny: the symbol fills more of them so it stays readable */
.grid[data-unit="weeks"] { --sym-inset: 8%; --halo-inset: 0%; }
.grid[data-unit="months"] { --gap: 3px; }
.grid[data-unit="years"] { --gap: 6px; --cell-r: 3px; }
.grid[data-unit="years"] .row { margin-top: 0; }

.row {
    display: grid;
    /* Squares take the size picked with the slider, and shrink when the screen is too narrow */
    grid-template-columns: var(--label-w, 28px) repeat(var(--cols), minmax(0, var(--size, 1fr)));
    gap: var(--gap, 2px);
    align-items: center;
}
.row.decade { margin-top: calc(var(--gap, 2px) * 3); }
.row:first-child { margin-top: 0; }

.row-label {
    font-family: 'Unbounded', system-ui, sans-serif;
    font-size: 0.6rem;
    color: var(--muted);
    text-align: right;
    padding-right: 6px;
}

.cell {
    aspect-ratio: 1;
    border-radius: var(--cell-r, 1.5px);
    background: var(--cell-empty);
    box-shadow: inset 0 0 0 var(--cell-bw, 1px) var(--cell-border);
    position: relative;
}
.cell.past { background: var(--cell-past); box-shadow: none; }
.cell.tint { background: color-mix(in srgb, var(--c) 30%, var(--cell-empty)); box-shadow: inset 0 0 0 var(--cell-bw, 1px) color-mix(in srgb, var(--c) 55%, var(--cell-empty)); }
.cell.past.tint { background: var(--c); box-shadow: none; }
.cell.now {
    background: var(--accent);
    box-shadow: none;
    animation: pulse 2.4s ease-in-out infinite;
}
.cell.moment::after {
    content: "";
    position: absolute;
    inset: var(--sym-inset, 20%);
    background: var(--m, var(--accent));
    -webkit-mask: var(--shape) center / contain no-repeat;
    mask: var(--shape) center / contain no-repeat;
    pointer-events: none;
}
/* The symbol sits inside its square, which keeps its own fill.
   On a filled square, a slightly bigger light copy behind it keeps it readable whatever the colours */
.cell.moment:is(.past, .tint, .now)::before {
    content: "";
    position: absolute;
    inset: var(--halo-inset, 12%);
    background: var(--cell-empty);
    -webkit-mask: var(--shape) center / contain no-repeat;
    mask: var(--shape) center / contain no-repeat;
    pointer-events: none;
}
.cell.moment[data-sym="ring"]:is(.past, .tint, .now)::before {
    --shape: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 10 10'%3E%3Ccircle cx='5' cy='5' r='4.1' fill='none' stroke='black' stroke-width='2.4'/%3E%3C/svg%3E");
}
.cell:hover { outline: 1.5px solid var(--ink); outline-offset: 1px; z-index: 1; }

@keyframes pulse {
    0%, 100% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--accent) 60%, transparent); }
    50% { box-shadow: 0 0 0 4px color-mix(in srgb, var(--accent) 0%, transparent); }
}

/* Rows fade in one after the other when the grid is (re)drawn */
.grid.intro .row { animation: rowIn 0.5s ease both; animation-delay: calc(var(--r) * 9ms); }
@keyframes rowIn { from { opacity: 0; transform: translateY(4px); } }

.legend {
    list-style: none;
    padding: 0;
    margin: 28px 0 0;
    display: flex;
    flex-wrap: wrap;
    gap: 10px 20px;
    font-size: 0.7rem;
    color: var(--muted);
}
.legend li { display: flex; align-items: center; gap: 8px; }
.legend i {
    width: 11px;
    height: 11px;
    border-radius: 2px;
    background: var(--c, var(--cell-empty));
    box-shadow: inset 0 0 0 1px var(--b, transparent);
}
.legend i.sym { border-radius: 0; box-shadow: none; }

.tip {
    position: fixed;
    z-index: 10;
    pointer-events: none;
    background: var(--ink);
    color: var(--bg);
    border-radius: 6px;
    padding: 8px 10px;
    font-size: 0.8rem;
    line-height: 1.45;
    max-width: 240px;
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.18);
}
.tip strong { display: block; font-size: 0.7rem; font-weight: 500; }
.tip span { display: block; opacity: 0.75; }
.tip em { display: block; font-style: normal; margin-top: 2px; }

@media (max-width: 860px) {
    .app { grid-template-columns: 1fr; }
    .panel { position: static; height: auto; border-right: 0; border-bottom: 1px solid var(--line); padding: 24px 16px; }
    .panel-foot { margin-top: 0; }
    .panel > * { width: auto; }
    /* Stacked: folding keeps the header and hides the settings below it */
    .app.collapsed { grid-template-columns: 1fr; }
    .app.collapsed .panel { visibility: visible; padding-inline: 16px; border-right-width: 0; }
    .app.collapsed .panel-body { display: none; }
    .app.collapsed .panel-open { display: none; }
    .app.collapsed .stage { padding-left: 16px; }
    #panel-toggle svg { transform: rotate(90deg); }
    .app.collapsed #panel-toggle svg { transform: rotate(-90deg); }
    .stage { padding: 24px 16px 48px; }
}

@media (max-width: 520px) {
    .grid { --gap: 1px; --label-w: 20px; --cell-bw: 0.5px; --cell-r: 1px; }
    .row-label { font-size: 0.5rem; padding-right: 3px; }
}

@media (prefers-reduced-motion: reduce) {
    .app { transition: none; }
    .grid.intro .row, .cell.now { animation: none; }
    .progress span { transition: none; }
}

@media print {
    @page { size: A4 portrait; margin: 12mm; }
    /* Always print on white, whatever the screen theme */
    :root, :root[data-theme], :root:not([data-theme="light"]) {
        --bg: #fff; --panel: #fff; --ink: #111; --muted: #666; --line: #ccc;
        --cell-empty: #fff; --cell-border: #bbb; --cell-past: #333;
    }
    .app { display: block; }
    .panel, .tip, .view-controls { display: none !important; }
    .stage { padding: 0; }
    .poster { max-width: none; }
    .grid { --gap: 1px; }
    .cell.now { animation: none; }
    .grid.intro .row { animation: none; }
    * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}
