:root {
    --bg: #f5f6f8;
    --surface: #ffffff;
    --surface-2: #f0f3f7;
    --ink: #1b2330;
    --muted: #5d6776;
    --line: #dde2e8;
    --accent: #002f6c;
    --accent-ink: #ffffff;
    --accent-soft: #e5ecf7;
    --red: #c8102e;
    --red-soft: #fbe9ec;
    --flag: #1f5fbf;
    --observance: #6b5aa6;
    --astro: #8a6d00;
    --moon-lit: #f3d36b;
    --moon-dark: #4a5363;
    --radius: 10px;
    --shadow: 0 1px 2px rgb(16 24 40 / .06), 0 1px 3px rgb(16 24 40 / .08);
    color-scheme: light;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #0f141b;
        --surface: #171e27;
        --surface-2: #1d2631;
        --ink: #e6ebf1;
        --muted: #9aa5b4;
        --line: #2a3440;
        --accent: #8db8ff;
        --accent-ink: #0f141b;
        --accent-soft: #1b2a3f;
        --red: #ff6b7d;
        --red-soft: #3a1c23;
        --flag: #8db8ff;
        --observance: #b7a6f0;
        --astro: #e0c35a;
        --moon-dark: #2c3542;
        --shadow: none;
        color-scheme: dark;
    }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
    margin: 0 auto;
    max-width: 1240px;
    padding: 0 16px 24px;
    background: var(--bg);
    color: var(--ink);
    font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

a { color: inherit; text-decoration: none; }
h1, h2, h3 { margin: 0; line-height: 1.2; }
.muted { color: var(--muted); }
.visually-hidden {
    position: absolute; width: 1px; height: 1px; overflow: hidden;
    clip: rect(0 0 0 0); white-space: nowrap;
}

select {
    font: inherit; color: var(--ink); background: var(--surface);
    border: 1px solid var(--line); border-radius: 8px; padding: .3rem .5rem;
}

a:focus-visible, select:focus-visible {
    outline: 2px solid var(--accent); outline-offset: 2px;
}

/* ---------- toolbar ---------- */

.toolbar {
    display: grid;
    grid-template-columns: 1fr auto;
    grid-template-areas: "brand brand" "pager tabs";
    gap: .75rem 1rem;
    align-items: center;
    padding: 14px 0 12px;
}

.brand { grid-area: brand; display: flex; align-items: center; justify-content: space-between; gap: 1rem; }
.brand-link { display: inline-flex; align-items: center; gap: .55rem; font-weight: 700; font-size: 1.25rem; letter-spacing: .01em; }
.brand-flag { width: 1.9rem; height: auto; border-radius: 2px; box-shadow: 0 0 0 1px var(--line); }

.pager { grid-area: pager; display: flex; align-items: center; gap: .4rem; min-width: 0; }
.pager-title { font-size: 1.35rem; font-weight: 650; min-width: 0; overflow-wrap: anywhere; padding: 0 .25rem; }
.pager-btn {
    display: inline-grid; place-items: center; flex: none;
    width: 2.2rem; height: 2.2rem; border-radius: 50%;
    font-size: 1.5rem; line-height: 1; background: var(--surface); border: 1px solid var(--line);
}
.pager-btn:hover, .today-btn:hover { border-color: var(--accent); }
.today-btn {
    flex: none; margin-left: .35rem; padding: .35rem .8rem; border-radius: 999px;
    border: 1px solid var(--line); background: var(--surface); font-weight: 600; font-size: .9rem;
}

.tabs { grid-area: tabs; display: inline-flex; background: var(--surface-2); border-radius: 999px; padding: 3px; }
.tab { padding: .35rem .9rem; border-radius: 999px; font-weight: 600; font-size: .9rem; color: var(--muted); }
.tab-active { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }

/* ---------- icons ---------- */

.flag-icon { width: 1.05rem; height: .65rem; flex: none; color: var(--ink); border-radius: 1px; }
.moon-icon { width: .95rem; height: .95rem; flex: none; }
.moon-large { width: 4.25rem; height: 4.25rem; }
.moon-dark { fill: var(--moon-dark); }
.moon-lit { fill: var(--moon-lit); }
.moon-rim { fill: none; stroke: var(--line); stroke-width: .6; }

.dot { display: inline-block; width: .45rem; height: .45rem; border-radius: 50%; background: var(--muted); flex: none; }
.dot-holiday { background: var(--red); }
.dot-dayoff { background: var(--red); opacity: .55; }
.dot-flag { background: var(--flag); }
.dot-observance { background: var(--observance); }

/* ---------- month view ---------- */

.month-layout {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 340px;
    grid-template-areas: "main side" "agenda side";
    gap: 1rem 1.25rem;
    align-items: start;
}
.month-main { grid-area: main; min-width: 0; }
.month-side { grid-area: side; position: sticky; top: 12px; }
.agenda { grid-area: agenda; }

.month-grid {
    display: grid;
    grid-template-columns: 2rem repeat(7, minmax(0, 1fr));
    grid-auto-rows: auto;
    gap: 4px;
    touch-action: pan-y;
}

.grid-head {
    font-size: .8rem; font-weight: 700; text-transform: uppercase; letter-spacing: .04em;
    color: var(--muted); text-align: center; padding: .2rem 0;
}
.grid-head.day-red { color: var(--red); }

.wk {
    display: grid; place-items: start center; padding-top: .45rem;
    font-size: .75rem; color: var(--muted); font-variant-numeric: tabular-nums;
}
a.wk:hover { color: var(--accent); }

.day {
    display: flex; flex-direction: column; gap: .2rem;
    min-height: 6.4rem; padding: .35rem .4rem .4rem;
    background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
    min-width: 0; overflow: hidden; position: relative;
}
.day:hover { border-color: var(--accent); }
.day-outside { opacity: .45; }
.day-top { display: flex; align-items: center; justify-content: space-between; gap: .25rem; }
.day-num {
    display: inline-grid; place-items: center; min-width: 1.75rem; height: 1.75rem; border-radius: 50%;
    font-weight: 650; font-size: 1rem; font-variant-numeric: tabular-nums;
}
.day-red .day-num, .day-red .week-card-date { color: var(--red); }
.day-today .day-num { background: var(--accent); color: var(--accent-ink); }
.day-selected { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); background: var(--accent-soft); }
.day-icons { display: inline-flex; align-items: center; gap: .25rem; }

.day-labels { display: flex; flex-direction: column; gap: .1rem; min-width: 0; font-size: .74rem; line-height: 1.25; }
.day-labels > span {
    display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; line-clamp: 2;
    overflow: hidden; overflow-wrap: break-word; hyphens: auto;
}
.label-holiday { color: var(--red); font-weight: 650; }
.label-dayoff { color: var(--red); }
.label-flag { color: var(--flag); }
.label-observance { color: var(--observance); }
.label-astro { color: var(--astro); }
.day-dots { display: none; }

.legend { display: flex; flex-wrap: wrap; gap: .4rem 1rem; margin-top: .6rem; font-size: .8rem; color: var(--muted); }
.legend span { display: inline-flex; align-items: center; gap: .35rem; }

/* ---------- day panel ---------- */

.day-panel {
    background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
    box-shadow: var(--shadow); overflow: hidden;
}
.day-head { padding: 1rem 1rem .75rem; background: var(--accent-soft); }
.day-head h2 { font-size: 1.2rem; }
.day-head p { margin: .3rem 0 0; font-size: .85rem; }
.day-head a { text-decoration: underline; text-underline-offset: 2px; }
.panel-section { padding: .8rem 1rem; border-top: 1px solid var(--line); }
.panel-section h3 { font-size: .95rem; }
.panel-section p { margin: .25rem 0 0; }

.event-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: .6rem; }
.event { display: flex; flex-wrap: wrap; align-items: baseline; gap: .2rem .45rem; }
.event-flag { display: grid; grid-template-columns: auto 1fr; align-items: center; column-gap: .5rem; }
.event-flag .event-meta { grid-column: 2; padding-left: 0; }
.event-name { font-weight: 600; }
.event-holiday .event-name, .event-dayoff .event-name { color: var(--red); }
.event-observance .event-name { color: var(--observance); }
.event-meta { flex-basis: 100%; font-size: .82rem; color: var(--muted); padding-left: 1.5rem; }
.event-time { color: var(--muted); font-size: .88rem; }
.tag { font-size: .7rem; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; padding: .1rem .4rem; border-radius: 4px; }
.tag-holiday, .tag-dayoff { background: var(--red-soft); color: var(--red); }

.section-head { display: flex; align-items: center; justify-content: space-between; gap: .5rem; margin-bottom: .4rem; }
.city-picker select { font-size: .85rem; padding: .2rem .4rem; }
.sun-row { display: flex; justify-content: space-between; gap: 1rem; padding: .15rem 0; }
.sun-row strong { font-variant-numeric: tabular-nums; }
.sun-polar { font-weight: 600; }
.moon-row { display: flex; align-items: center; gap: 1rem; }

/* ---------- agenda ---------- */

.agenda h2 { font-size: 1.05rem; margin: .25rem 0 .5rem; }
.agenda ul { list-style: none; margin: 0; padding: 0; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
.agenda-row + .agenda-row { border-top: 1px solid var(--line); }
.agenda-row a { display: grid; grid-template-columns: 11.5rem 1fr; gap: .75rem; padding: .5rem .85rem; }
.agenda-row a:hover { background: var(--surface-2); }
.agenda-date { font-weight: 600; font-variant-numeric: tabular-nums; }
.agenda-red .agenda-date { color: var(--red); }
.agenda-items { display: flex; flex-wrap: wrap; gap: .1rem .9rem; font-size: .9rem; }

/* ---------- week view ---------- */

.week-grid { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: .6rem; touch-action: pan-y; }
.week-card {
    display: flex; flex-direction: column; gap: .5rem; min-height: 12rem; padding: .75rem;
    background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); min-width: 0;
}
.week-card:hover { border-color: var(--accent); }
.week-card.day-today { box-shadow: inset 0 0 0 2px var(--accent); }
.week-card-head { display: flex; justify-content: space-between; align-items: flex-start; gap: .4rem; }
.week-card-date { font-weight: 700; }
.week-card .day-labels { font-size: .82rem; flex: 1; }
.week-card .day-labels > span { -webkit-line-clamp: unset; line-clamp: none; }
.week-card-sun { font-size: .8rem; font-variant-numeric: tabular-nums; }

/* ---------- year view ---------- */

.year-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: .9rem; }
.mini-month { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: .7rem .75rem .8rem; }
.mini-month h3 { font-size: 1rem; margin-bottom: .4rem; }
.mini-month h3 a:hover { color: var(--accent); }
.mini-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 2px; text-align: center; font-size: .82rem; font-variant-numeric: tabular-nums; }
.mini-head { color: var(--muted); font-size: .7rem; font-weight: 700; }
.mini-day { padding: .2rem 0; border-radius: 6px; position: relative; }
.mini-day:hover { background: var(--surface-2); }
.mini-day.day-red { color: var(--red); font-weight: 650; }
.mini-day.day-today { background: var(--accent); color: var(--accent-ink); }
.mini-flag::after {
    content: ""; position: absolute; left: 30%; right: 30%; bottom: 1px; height: 2px; border-radius: 1px; background: var(--flag);
}
.mini-fullmoon::before {
    content: ""; position: absolute; top: 2px; right: 2px; width: 5px; height: 5px; border-radius: 50%; background: var(--moon-lit);
}

/* ---------- footer ---------- */

.footer { margin-top: 2rem; font-size: .8rem; color: var(--muted); border-top: 1px solid var(--line); padding-top: .75rem; }
.footer p { margin: .25rem 0; }

/* ---------- responsive ---------- */

@media (max-width: 1099px) {
    .week-grid { grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); }
    .week-card { min-height: 0; }
}

@media (max-width: 899px) {
    .month-layout { grid-template-columns: minmax(0, 1fr); grid-template-areas: "main" "side" "agenda"; }
    .month-side { position: static; scroll-margin-top: 8px; }
}

@media (max-width: 640px) {
    body { font-size: 14px; }
    .toolbar { grid-template-columns: minmax(0, 1fr); grid-template-areas: "brand" "pager" "tabs"; gap: .6rem; }
    .tabs { display: grid; grid-template-columns: repeat(3, 1fr); text-align: center; }
    .pager-title { font-size: 1.15rem; flex: 1; text-align: center; }
    .brand-link { font-size: 1.1rem; }
    .lang select { font-size: .85rem; padding: .25rem .3rem; }
    .grid-head { font-size: .68rem; letter-spacing: 0; }
    .event-meta { padding-left: 0; }
    .month-grid { grid-template-columns: 1.4rem repeat(7, minmax(0, 1fr)); gap: 3px; }
    .wk { font-size: .65rem; padding-top: .55rem; }
    .day { min-height: 3.3rem; padding: .2rem; align-items: center; gap: .1rem; border-radius: 8px; }
    .day-top { flex-direction: column; gap: 0; }
    .day-num { min-width: 1.6rem; height: 1.6rem; font-size: .92rem; }
    .day-icons { display: none; }
    .day-labels { display: none; }
    .day-dots { display: flex; gap: 2px; align-items: center; justify-content: center; min-height: .5rem; }
    .day-dots .moon-icon { width: .55rem; height: .55rem; }
    .agenda-row a { grid-template-columns: 1fr; gap: .1rem; }
    .week-grid { grid-template-columns: minmax(0, 1fr); }
}

@media print {
    body { background: #fff; color: #000; max-width: none; }
    .tabs, .pager-btn, .today-btn, .lang, .month-side, .footer, .city-picker { display: none !important; }
    .month-layout { grid-template-columns: 1fr; grid-template-areas: "main" "agenda"; }
    .day, .mini-month, .agenda ul { box-shadow: none; break-inside: avoid; }
}
