/* ==========================================================================
   Vuosikello – tyylit
   Rakenne: 1. teema  2. perusasettelu  3. välilehdet  4. kello  5. listat ja kortit
            6. lomakkeet  7. kalenterivalitsin  8. asetukset  9. responsiivisuus
   ========================================================================== */

/* ---------- 1. Teema ----------
   Värit on määritelty light-dark()-funktiolla (vaalea, tumma). Oletuksena teema seuraa
   järjestelmää; Asetuksista valittu teema asetetaan html-elementin data-theme-attribuuttiin. */
:root {
  color-scheme: light dark;
  box-sizing: border-box;
  padding-top: env(safe-area-inset-top, 0px);       /* puhelimen lovi / tilarivi */
  padding-bottom: env(safe-area-inset-bottom, 0px);

  --bg:      light-dark(#f4f6f8, #14191d);   /* sivun tausta */
  --surface: light-dark(#fff, #1c2328);      /* korttien ja kenttien pinta */
  --ink:     light-dark(#27323a, #e9eef1);   /* pääteksti */
  --muted:   light-dark(#667580, #9aa8b1);   /* toissijainen teksti */
  --line:    light-dark(#dbe2e7, #2f3a42);   /* viivat ja reunat */
  --accent:  light-dark(#e2574c, #ff7a6e);   /* korostus (tämän päivän viiva) */
  --late:    light-dark(#d92d20, #ff6b5e);   /* myöhässä */
  --m1:      light-dark(#e9eef2, #27323a);   /* kuukausirengas, parilliset */
  --m2:      light-dark(#dde5ea, #303d46);   /* kuukausirengas, parittomat */
  --sel:     light-dark(#27323a, #e9eef1);   /* valitun kuukauden tausta */
  --selink:  light-dark(#fff, #14191d);      /* valitun kuukauden teksti */
}
:root[data-theme="light"] { color-scheme: light; }
:root[data-theme="dark"]  { color-scheme: dark; }

/* ---------- 2. Perusasettelu ---------- */
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--ink);
  font-family: "Nunito", system-ui, -apple-system, "Segoe UI", sans-serif;
  line-height: 1.45;
}
main { max-width: 620px; margin: 0 auto; padding: 16px 14px 48px; }
h2 { font-size: 17px; font-weight: 800; margin: 20px 0 8px; }
h3 { font-size: 13px; font-weight: 800; color: var(--muted); margin: 14px 0 6px; text-transform: uppercase; letter-spacing: .04em; }

.hide { display: none !important; }     /* piilottaa elementin (JS vaihtaa tätä luokkaa) */
.empty { color: var(--muted); margin: 4px 0; }
.flush { margin: 0; }
.full { width: 100%; }
.stack { display: grid; gap: 10px; }

/* Välilehtien sisältö on keskitetty kapeaan palstaan; vain kello käyttää koko leveyden */
#up, #add, #set, #hist { max-width: 680px; margin-left: auto; margin-right: auto; }

/* ---------- 3. Välilehdet ja valitsimet ---------- */
.tabs, .seg2 {
  display: flex; gap: 3px; margin-bottom: 12px; padding: 3px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px;
}
.tabs { max-width: 680px; margin-left: auto; margin-right: auto; }
.tabs button, .seg2 button {
  flex: 1; padding: 9px 4px; border: 0; border-radius: 9px; background: none;
  color: var(--ink); font: inherit; font-weight: 800; cursor: pointer;
}
.tabs button.on, .seg2 button.on { background: var(--ink); color: var(--bg); }
.tabs button[data-t=set], .tabs button[data-t=hist] { flex: 0 0 54px; font-size: 18px; } /* pelkkä kuvake */

/* Myöhässä-banneri */
.lateban {
  display: block; width: 100%; max-width: 680px; margin: 0 auto 12px; padding: 10px 14px;
  border: 1.5px solid var(--late); border-radius: 12px;
  background: color-mix(in srgb, var(--late) 8%, var(--surface));
  color: var(--late); font: inherit; font-weight: 800; text-align: left; cursor: pointer;
}

/* ---------- 4. Kello (SVG) ---------- */
#svg { width: 100%; height: auto; display: block; margin: 0 auto; }

.mseg { fill: var(--m1); cursor: pointer; stroke: var(--bg); stroke-width: 1.5; }   /* kuukausi-/neljännesrengas */
.mseg.odd { fill: var(--m2); }
.mseg.on  { fill: var(--sel); }
.mt { font-size: 13px; font-weight: 800; fill: var(--ink); pointer-events: none; letter-spacing: .03em; }
.mt.on { fill: var(--selink); }

.act { cursor: pointer; stroke: var(--surface); stroke-width: 1; }                  /* tapahtumakaari */
.act.sel { stroke: var(--ink); stroke-width: 2.5; }
.act.late { stroke: var(--late); stroke-width: 2.6; }
.act.late.sel { stroke: var(--ink); stroke-width: 3; }
.atx  { fill: #fff; font-weight: 700; pointer-events: none; }                       /* teksti kaaren päällä */
.atx2 { fill: var(--ink); font-weight: 700; pointer-events: none; }                 /* teksti kaaren vieressä */

.legend { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; margin: 8px 0; }
.legend .chip {
  display: flex; align-items: center; gap: 6px; padding: 4px 10px 4px 8px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 20px;
  color: var(--ink); font: inherit; font-weight: 600; font-size: 14px; cursor: pointer;
}
.legend .chip i { width: 12px; height: 12px; border-radius: 50%; flex: none; }
.legend .chip.off { opacity: .45; text-decoration: line-through; }   /* renkaan voi piilottaa napauttamalla */

/* Kuukausilistan taittonappi */
.fold {
  display: flex; justify-content: space-between; align-items: center; width: 100%;
  margin: 20px 0 8px; padding: 6px 0; background: none; border: 0; border-bottom: 1px solid var(--line);
  font: inherit; font-size: 17px; font-weight: 800; color: var(--ink); text-align: left; cursor: pointer;
}
.fold #arr { color: var(--muted); font-size: 14px; }

/* ---------- 5. Listat ja kortit ---------- */
.ev {
  display: flex; align-items: center; gap: 10px; margin-bottom: 8px; padding: 10px 12px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px;
}
.ev.done b { text-decoration: line-through; color: var(--muted); }
.ev.late { border-color: var(--late); background: color-mix(in srgb, var(--late) 8%, var(--surface)); }
.ev .txt { flex: 1; min-width: 0; }
.ev small { display: block; color: var(--muted); }
.ev small.dsc1 { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }   /* selite yhdellä rivillä */
.ev .ed { padding: 4px 8px; border: 0; background: none; color: var(--muted); font-size: 18px; cursor: pointer; }
.dot { width: 12px; height: 12px; border-radius: 50%; flex: none; }
.chk { width: 24px; height: 24px; flex: none; accent-color: var(--accent); cursor: pointer; }
.badge { color: var(--late); font-weight: 800; }          /* "Myöhässä 3 pv" */
.okt { color: var(--muted); font-weight: 700; }          /* "✓ Tehty ..." */
.dsc { margin: 6px 0 0; font-size: 14px; white-space: pre-wrap; overflow-wrap: anywhere; }

.btns { display: flex; gap: 8px; margin: -2px 0 8px; }
.sm {
  padding: 8px 14px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface);
  color: var(--ink); font: inherit; font-weight: 700; font-size: 14px; cursor: pointer;
}
.sm.dng { color: var(--accent); }

/* ---------- 6. Lomakkeet ---------- */
.form {
  display: grid; gap: 10px; padding: 14px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 14px;
}
label { display: grid; gap: 4px; font-size: 14px; color: var(--muted); }
.form input, .form select, .form textarea {
  width: 100%; padding: 10px; border: 1px solid var(--line); border-radius: 10px;
  background: var(--bg); color: var(--ink); font: inherit; font-size: 16px;
}
.form textarea { resize: vertical; }
#endwrap { display: grid; gap: 10px; }

.add {   /* ensisijainen painike */
  padding: 12px; border: 0; border-radius: 10px; background: var(--ink); color: var(--bg);
  font: inherit; font-weight: 800; font-size: 16px; cursor: pointer;
}
.add.alt { background: var(--surface); color: var(--ink); border: 1px solid var(--line); }
.add.danger, .danger { color: var(--accent); }

/* ---------- 7. Kalenterivalitsin ---------- */
.cal { padding: 10px; border: 1px solid var(--line); border-radius: 12px; background: var(--bg); }
.calm { display: flex; gap: 6px; margin-bottom: 8px; }
.calm button {
  flex: 1; padding: 7px; border: 1px solid var(--line); border-radius: 9px; background: var(--surface);
  color: var(--ink); font: inherit; font-weight: 700; font-size: 14px; cursor: pointer;
}
.calm button.on { background: var(--ink); color: var(--bg); }
.calh { display: flex; align-items: center; justify-content: space-between; margin-bottom: 6px; }
.calh button {
  width: 36px; height: 36px; border: 1px solid var(--line); border-radius: 50%;
  background: var(--surface); color: var(--ink); font-size: 18px; cursor: pointer;
}
.calg { display: grid; grid-template-columns: repeat(7, 1fr); gap: 3px; text-align: center; }
.calg i { padding: 2px 0; font-style: normal; font-size: 12px; font-weight: 700; color: var(--muted); }
.cd { aspect-ratio: 1; padding: 0; border: 0; border-radius: 8px; background: none; color: var(--ink); font: inherit; font-size: 15px; cursor: pointer; }
.cd.rng { background: color-mix(in srgb, var(--ink) 13%, transparent); border-radius: 0; }   /* valittu väli */
.cd.sel { background: var(--ink); color: var(--bg); font-weight: 800; border-radius: 8px; }  /* alku- ja loppupäivä */
.cd.td  { outline: 1.5px solid var(--accent); outline-offset: -1.5px; }                      /* tänään */
.calb { display: flex; gap: 8px; justify-content: flex-end; margin-top: 10px; }
.calb button {
  padding: 8px 14px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface);
  color: var(--ink); font: inherit; font-weight: 700; font-size: 14px; cursor: pointer;
}
.calb #caldone { background: var(--ink); color: var(--bg); }

/* ---------- 8. Asetukset: renkaiden muokkaus ---------- */
#set > h2:first-child { margin-top: 4px; }
.rr {
  display: flex; align-items: center; gap: 8px; margin-bottom: 8px; padding: 8px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px;
}
.rr input[type=color] { width: 38px; height: 38px; padding: 0; border: 0; background: none; flex: none; cursor: pointer; }
.rr input[type=text] {
  flex: 1; min-width: 0; padding: 8px 10px; border: 1px solid var(--line); border-radius: 10px;
  background: var(--bg); color: var(--ink); font: inherit; font-size: 16px;
}
.rr button {
  min-width: 34px; height: 36px; border: 1px solid var(--line); border-radius: 9px;
  background: var(--surface); color: var(--ink); font-size: 15px; cursor: pointer;
}
.rr button:disabled { opacity: .35; cursor: default; }
.rr .rx { color: var(--accent); }

/* ---------- 8b. Lisäosat ---------- */
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; align-items: end; }
.plain, input.q {
  width: 100%; padding: 10px; border: 1px solid var(--line); border-radius: 10px;
  background: var(--surface); color: var(--ink); font: inherit; font-size: 16px;
}
input.q { margin-bottom: 10px; }
label.check { display: flex; align-items: center; gap: 8px; color: var(--ink); }
label.check input { width: auto; }
hr { width: 100%; border: 0; border-top: 1px solid var(--line); margin: 4px 0; }
.rob { margin: 0 auto 12px; max-width: 680px; padding: 8px 14px; border-radius: 12px; background: var(--surface); border: 1px solid var(--line); font-weight: 700; }
.mt.hol { fill: var(--late); }                                       /* pyhäpäivä */
.wk { font-size: 8px; font-weight: 700; fill: var(--muted); pointer-events: none; }   /* viikkonumero */
.hdot { fill: var(--late); pointer-events: none; }
.dseg { cursor: pointer; }
#svg { touch-action: pan-y; }
#svg [tabindex]:focus { outline: none; }
.act:focus-visible { stroke: var(--ink); stroke-width: 3; }
.mseg:focus-visible { stroke: var(--accent); stroke-width: 3; }
.nav:focus-visible circle:nth-child(2), .cz:focus-visible { stroke: var(--accent); stroke-width: 3; }
.ev .snz { padding: 4px 6px; border: 0; background: none; font-size: 16px; cursor: pointer; }
.snzopts { display: inline-flex; gap: 4px; }
.snzopts button { padding: 4px 8px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); color: var(--ink); font: inherit; font-size: 12px; font-weight: 700; cursor: pointer; }
.ev small.meta a { color: inherit; }
.cl { margin: 6px 0 0; padding: 0; list-style: none; }
.cl li { display: flex; align-items: center; gap: 8px; padding: 2px 0; }
.cl input { width: 18px; height: 18px; accent-color: var(--accent); }
.cl li.on span { text-decoration: line-through; color: var(--muted); }
.prow { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; padding: 8px 10px; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; }
.prow b { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.prow.cur { border-color: var(--ink); }
.toast {
  position: fixed; left: 50%; bottom: calc(16px + env(safe-area-inset-bottom, 0px)); transform: translateX(-50%); z-index: 50;
  display: flex; align-items: center; gap: 14px; max-width: calc(100vw - 24px); padding: 10px 14px;
  background: var(--ink); color: var(--bg); border-radius: 12px; box-shadow: 0 6px 24px rgba(0,0,0,.25); font-weight: 700;
}
.toast button { background: none; border: 0; color: inherit; font: inherit; font-weight: 800; text-decoration: underline; cursor: pointer; }
.dragtip { position: fixed; z-index: 60; padding: 4px 8px; background: var(--ink); color: var(--bg); border-radius: 8px; font-weight: 700; font-size: 13px; pointer-events: none; }
body.readonly #add-tab, body.readonly [data-t=add], body.readonly .ed, body.readonly .btns, body.readonly .snz { display: none !important; }
body.readonly .chk { pointer-events: none; opacity: .6; }

/* ---------- 9. Responsiivisuus ---------- */
@media (min-width: 700px) {
  main { max-width: 760px; }
  #svg { max-height: calc(100vh - 190px); }       /* kello mahtuu näytön korkeuteen */
}
@media (min-width: 1100px) {
  /* Työpöytä: kello vasemmalla, kortti ja kuukausilista oikealla */
  main { max-width: 1560px; padding: 20px 32px 48px; }
  #wheel {
    display: grid; grid-template-columns: minmax(0, 1fr) 420px; grid-template-rows: auto auto auto auto 1fr;
    column-gap: 40px; align-items: start;
  }
  #wheel > #svg, #wheel > .legend { grid-column: 1; }
  #wheel > .side { grid-column: 2; grid-row: 1 / span 5; }
  .side .fold:first-child { margin-top: 0; }
}
@media (min-width: 1700px) {
  main { max-width: 1800px; }
}

/* ---------- 10. Tulostus (PDF) ---------- */
@media print {
  :root { color-scheme: light; }
  .tabs, .lateban, .rob, .toast, #fold, .btns, .ed, .snz, .chk { display: none !important; }
  #up, #add, #set, #hist { display: none !important; }
  #wheel { display: block !important; }
  #svg { max-height: 70vh; }
  main { max-width: none; padding: 0; }
}
