:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #eef1f5;
  --text: #1c2430;
  --muted: #5d6878;
  --border: #d8dde5;
  --accent: #1f3a5f;
  --accent-text: #ffffff;
  --head: #34495e;
  --today: #d9480f;
  --empty: #dfe3ea;

  --c-equinox: #fff176;
  --c-newmonth: #d9ead3;
  --c-sabbath: #d9d2e9;
  --c-feast: #c9daf8;
  --c-fast: #cfcfcf;
  --c-dss: #fce5cd;
  --c-day31: #f4cccc;
  --c-season: #fff2cc;

  --s-spring: #6aa84f;
  --s-summer: #e6b31e;
  --s-fall: #d9534f;
  --s-winter: #5b8fd9;

  --radius: 10px;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 4px 16px rgba(0,0,0,.06);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1419; --surface: #182029; --surface-2: #1f2a35; --text: #e6ebf1; --muted: #9aa7b6;
    --border: #2c3946; --accent: #8fb4e3; --accent-text: #0f1419; --head: #263443; --today: #ff8a50; --empty: #141b22;
    --c-equinox: #6b6114; --c-newmonth: #2e4a2c; --c-sabbath: #3d3558; --c-feast: #2a4066; --c-fast: #44484d;
    --c-dss: #5a4228; --c-day31: #5c2f2f; --c-season: #4d4425;
    --shadow: 0 1px 2px rgba(0,0,0,.4); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1419; --surface: #182029; --surface-2: #1f2a35; --text: #e6ebf1; --muted: #9aa7b6;
  --border: #2c3946; --accent: #8fb4e3; --accent-text: #0f1419; --head: #263443; --today: #ff8a50; --empty: #141b22;
  --c-equinox: #6b6114; --c-newmonth: #2e4a2c; --c-sabbath: #3d3558; --c-feast: #2a4066; --c-fast: #44484d;
  --c-dss: #5a4228; --c-day31: #5c2f2f; --c-season: #4d4425;
  --shadow: 0 1px 2px rgba(0,0,0,.4); color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  padding-bottom: env(safe-area-inset-bottom);
}
button, select, input { font: inherit; color: inherit; }
h1, h2, h3 { margin: 0; }

/* ---------- Barra superior ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: calc(8px + env(safe-area-inset-top)) 16px 8px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; margin-right: auto; min-width: 0; }
.brand h1 { font-size: 17px; }
.brand .sub { margin: 0; font-size: 12px; color: var(--muted); }
.yearnav { display: flex; align-items: center; gap: 4px; }
.yearnav select {
  padding: 6px 8px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface-2);
  font-weight: 600; font-variant-numeric: tabular-nums;
}
.actions { display: flex; gap: 6px; align-items: center; }
.btn {
  border: 1px solid var(--accent); background: var(--accent); color: var(--accent-text);
  padding: 6px 12px; border-radius: 8px; cursor: pointer; font-weight: 600;
}
.btn.ghost { background: transparent; color: var(--accent); }
.icon-btn {
  border: 1px solid var(--border); background: var(--surface-2); border-radius: 8px;
  min-width: 36px; height: 36px; cursor: pointer; font-size: 18px; line-height: 1;
}
.icon-btn:hover, .btn:hover { filter: brightness(1.08); }

.tabs {
  display: flex; gap: 4px; padding: 8px 16px 0; overflow-x: auto; scrollbar-width: none;
}
.tabs button {
  flex: 0 0 auto; border: 1px solid var(--border); background: var(--surface); border-radius: 999px;
  padding: 6px 14px; cursor: pointer; color: var(--muted); font-weight: 600;
}
.tabs button[aria-selected="true"] { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }

.yearinfo {
  display: flex; flex-wrap: wrap; gap: 6px 16px; padding: 10px 16px 0; font-size: 13px; color: var(--muted);
}
.yearinfo b { color: var(--text); font-weight: 600; }
.yearinfo .warn { color: var(--today); font-weight: 600; }

.layout { display: grid; grid-template-columns: minmax(0, 1fr); gap: 16px; padding: 12px 16px; }
@media (min-width: 1100px) {
  .layout.with-pane { grid-template-columns: minmax(0, 1fr) 360px; }
  .detail-pane:not([hidden]) {
    display: block; position: sticky; top: 76px; align-self: start;
    max-height: calc(100vh - 92px); overflow: auto;
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
  }
}
@media (max-width: 1099px) { .detail-pane { display: none !important; } }

.credit { padding: 8px 16px 24px; text-align: center; font-size: 12px; color: var(--muted); }
.loading { color: var(--muted); }

/* ---------- Encabezado de periodo ---------- */
.period { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.period h2 { font-size: 20px; flex: 1; text-align: center; }
.period .sub { display: block; font-size: 12px; font-weight: 400; color: var(--muted); }
.chips { display: flex; gap: 4px; overflow-x: auto; padding-bottom: 8px; margin-bottom: 4px; scrollbar-width: thin; }
.chips button {
  flex: 0 0 auto; border: 1px solid var(--border); background: var(--surface); border-radius: 8px;
  padding: 4px 10px; cursor: pointer; font-size: 13px; font-variant-numeric: tabular-nums;
  border-bottom: 3px solid var(--season, var(--border));
}
.chips button[aria-current="true"] { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }

/* ---------- Cuadrícula mensual ---------- */
.grid {
  display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 1px;
  background: var(--border); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden;
  box-shadow: var(--shadow);
}
.grid .wh {
  background: var(--head); color: #fff; text-align: center; padding: 6px 2px; font-size: 12px; font-weight: 600;
  line-height: 1.2;
}
.grid .wh small { display: block; font-weight: 400; opacity: .8; }
.cell {
  position: relative; background: var(--surface); min-height: 96px; padding: 5px 6px; text-align: left;
  border: 0; cursor: pointer; display: flex; flex-direction: column; gap: 2px; overflow: hidden;
}
.cell:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; z-index: 1; }
.cell.empty { background: var(--empty); cursor: default; }
.cell.out { opacity: .55; }
.cell .num { font-weight: 700; font-size: 16px; line-height: 1; }
.cell .alt { font-size: 11px; color: var(--muted); }
.cell .short { display: none; }
.cell .lbl { font-size: 11.5px; line-height: 1.2; }
.cell .lbl.strong { font-weight: 600; }
.cell .ev { font-size: 10.5px; color: var(--muted); line-height: 1.2; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.cell .dots { display: flex; gap: 3px; margin-top: auto; }
.cell .dot { width: 7px; height: 7px; border-radius: 50%; border: 1px solid rgba(0,0,0,.15); }
.cell.today::after {
  content: ""; position: absolute; inset: 1px; border: 2.5px solid var(--today); border-radius: 6px; pointer-events: none;
}
.cell.selected { box-shadow: inset 0 0 0 2px var(--accent); }

.cat-equinox { background: var(--c-equinox); }
.cat-newmonth { background: var(--c-newmonth); }
.cat-sabbath { background: var(--c-sabbath); }
.cat-feast { background: var(--c-feast); }
.cat-fast { background: var(--c-fast); }
.cat-dss { background: var(--c-dss); }
.cat-day31 { background: var(--c-day31); }
.cat-season { background: var(--c-season); }

@media (max-width: 700px) {
  .layout { padding: 10px 8px; }
  .grid .wh { font-size: 10px; padding: 4px 1px; }
  .cell { min-height: 64px; padding: 3px 3px; }
  .cell .num { font-size: 14px; }
  .cell .alt { font-size: 9.5px; }
  .cell .lbl { font-size: 9.5px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
  .cell .lbl ~ .lbl { display: none; }
  .cell .full { display: none; }
  .cell .short { display: inline; }
  .cell .ev { display: none; }
  .brand .sub { display: none; }
  .topbar { gap: 8px; padding-left: 10px; padding-right: 10px; }
  .yearinfo { font-size: 12px; gap: 2px 12px; padding: 8px 10px 0; }
  .tabs { padding: 8px 10px 0; }
  .period h2 { font-size: 17px; }
}

/* ---------- Vista de año (1 página) ---------- */
.yeargrid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)) auto; gap: 8px; }
@media (max-width: 1200px) { .yeargrid { grid-template-columns: repeat(2, minmax(0, 1fr)); } .yeargrid .tab-col { grid-column: 1 / -1; } }
@media (max-width: 560px) { .yeargrid { grid-template-columns: minmax(0, 1fr); } }
.quarter { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 1px; background: var(--border);
  border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); align-self: start; }
.mh { color: #fff; text-align: center; font-size: 12px; font-weight: 700; padding: 5px 2px; line-height: 1.15; cursor: pointer; }
.mh small { display: block; font-weight: 400; opacity: .9; }
.yc {
  background: var(--surface); border: 0; display: grid; grid-template-columns: 2.6em 1.6em 1fr; align-items: center;
  font-size: 11.5px; padding: 2px 4px; text-align: left; cursor: pointer; min-height: 22px; position: relative;
  font-variant-numeric: tabular-nums;
}
.yc .g { color: var(--muted); font-size: 10.5px; }
.yc .b { font-weight: 700; text-align: right; padding-right: 4px; }
.yc .l { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.yc.empty { background: var(--empty); cursor: default; }
.yc.today { outline: 2px solid var(--today); outline-offset: -2px; z-index: 1; }
.tab-col { display: flex; flex-direction: column; gap: 1px; min-width: 150px; background: var(--border);
  border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; align-self: start; }
.tab-col .mh { background: #b59b00; }
.yc.wide { grid-template-columns: 3.4em 1fr; }

/* ---------- Lista de fiestas ---------- */
.feast-tools { display: flex; gap: 6px; margin-bottom: 10px; flex-wrap: wrap; }
.feast-tools button { border: 1px solid var(--border); background: var(--surface); border-radius: 999px; padding: 4px 12px; cursor: pointer; }
.feast-tools button[aria-pressed="true"] { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.flist { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.flist h3 { font-size: 14px; color: var(--muted); margin: 12px 0 2px; padding-left: 10px; border-left: 4px solid var(--season, var(--border)); }
.fitem {
  display: grid; grid-template-columns: 14px minmax(0, 1fr) auto; gap: 10px; align-items: center; width: 100%;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 12px;
  text-align: left; cursor: pointer;
}
.fitem .sw { width: 14px; height: 14px; border-radius: 4px; border: 1px solid rgba(0,0,0,.15); }
.fitem .nm { font-weight: 600; }
.fitem .meta { font-size: 12.5px; color: var(--muted); }
.fitem .when { text-align: right; font-size: 13px; font-variant-numeric: tabular-nums; }
.fitem.past { opacity: .6; }
.fitem.next { border-color: var(--today); box-shadow: 0 0 0 1px var(--today); }
.badge { display: inline-block; font-size: 11px; padding: 1px 6px; border-radius: 999px; background: var(--today); color: #fff; margin-left: 6px; vertical-align: middle; }

/* ---------- Detalle del día ---------- */
.detail { padding: 16px; }
.detail .dh { display: flex; align-items: flex-start; gap: 8px; margin-bottom: 10px; }
.detail .dh h2 { font-size: 19px; flex: 1; }
.detail .dh .sub { display: block; font-size: 13px; font-weight: 400; color: var(--muted); margin-top: 2px; }
.detail .marks { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.detail .mk { padding: 3px 9px; border-radius: 999px; font-size: 13px; font-weight: 600; border: 1px solid rgba(0,0,0,.08); }
.detail dl { display: grid; grid-template-columns: auto 1fr; gap: 6px 12px; margin: 0 0 14px; font-size: 14px; }
.detail dt { color: var(--muted); }
.detail dd { margin: 0; }
.detail h3 { font-size: 14px; margin: 14px 0 6px; }
.detail .txt { white-space: pre-wrap; font-size: 14px; line-height: 1.5; background: var(--surface-2); border-radius: 8px; padding: 10px 12px; overflow-wrap: anywhere; }
.detail .note { font-size: 12px; color: var(--muted); margin-top: 6px; }
.detail .nav { display: flex; gap: 8px; margin-top: 14px; }
.detail .nav button { flex: 1; }

/* ---------- Diálogos ---------- */
dialog.sheet {
  border: 0; padding: 0; background: var(--surface); color: var(--text); border-radius: 14px; box-shadow: 0 10px 40px rgba(0,0,0,.3);
  width: min(560px, calc(100vw - 32px)); max-height: calc(100vh - 48px); overflow: auto;
}
dialog.sheet.small { width: min(460px, calc(100vw - 32px)); }
dialog::backdrop { background: rgba(10, 15, 20, .5); }
@media (max-width: 700px) {
  dialog.sheet { margin: auto 0 0; width: 100vw; max-width: 100vw; border-radius: 16px 16px 0 0; max-height: 88vh;
    padding-bottom: env(safe-area-inset-bottom); }
}
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px; border-bottom: 1px solid var(--border);
  position: sticky; top: 0; background: var(--surface); z-index: 1; }
.legend { list-style: none; margin: 0; padding: 12px 16px 16px; display: grid; gap: 8px; }
.legend li { display: grid; grid-template-columns: 28px 1fr; gap: 10px; align-items: center; font-size: 14px; }
.legend .sw { width: 28px; height: 18px; border-radius: 4px; border: 1px solid rgba(0,0,0,.12); }

.settings { padding: 12px 16px 20px; display: grid; gap: 16px; }
.settings fieldset { border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 12px; margin: 0; display: grid; gap: 8px; }
.settings legend { font-weight: 700; padding: 0 4px; }
.settings label { display: flex; gap: 8px; align-items: center; }
.settings .help { font-size: 12.5px; color: var(--muted); margin: 0; }
.settings select, .settings input[type="date"] { padding: 6px 8px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface-2); max-width: 100%; }
.settings .row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg label { padding: 5px 12px; cursor: pointer; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg label:has(input:checked) { background: var(--accent); color: var(--accent-text); }

@media print {
  .topbar, .tabs, .credit, .detail-pane, .chips { display: none !important; }
  .layout { padding: 0; }
  .cell { min-height: 70px; }
}

@media (max-width: 520px) {
  .brand { display: none; }
  .brand { margin-right: 0; }
  .yearnav { margin-right: auto; }
  .yearnav select { padding: 6px 4px; }
  .actions .btn { padding: 6px 10px; }
}
