/* ==========================================================================
   Visuaalinen ajastin – tyylit
   ========================================================================== */

:root {
  --primary: #0066cc;
  --primary-dark: #004c99;
  --bg: #f5f7fa;
  --surface: #ffffff;
  --text: #2d3748;
  --text-muted: #718096;
  --border: #e2e8f0;
  --danger: #e53e3e;
  --danger-dark: #c53030;
  --success: #38a169;
  --accent: #ff6b6b;
  --accent-dark: #e53e3e;
  --timer-circle-bg: rgba(0, 102, 204, 0.08);
  --timer-circle-fill: #0066cc;
  --timer-bar-bg: rgba(0, 102, 204, 0.1);
  --timer-bar-fill: #0066cc;
  --tick-minor: rgba(45, 55, 72, 0.28);
  --tick-major: rgba(45, 55, 72, 0.55);
  --focus-ring: 0 0 0 3px rgba(0, 102, 204, 0.25);
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.12);
  --shadow-md: 0 4px 6px rgba(0, 0, 0, 0.1);
  --shadow-lg: 0 10px 15px rgba(0, 0, 0, 0.1);
  --radius: 8px;
  /* Ajastimen täyttöväri: punaistuu viimeisen minuutin aikana (.ending) */
  --timer-fill: var(--timer-circle-fill);
}

[data-theme="dark"] {
  --bg: #0f172a;
  --surface: #1e293b;
  --text: #f1f5f9;
  --text-muted: #94a3b8;
  --border: #334155;
  --timer-circle-bg: rgba(59, 130, 246, 0.15);
  --timer-circle-fill: #3b82f6;
  --timer-bar-bg: rgba(59, 130, 246, 0.2);
  --timer-bar-fill: #3b82f6;
  --tick-minor: rgba(241, 245, 249, 0.25);
  --tick-major: rgba(241, 245, 249, 0.5);
}

/* Viimeinen minuutti: täyttö muuttuu punaiseksi */
.timer-wrap.ending { --timer-fill: var(--danger); }

/* ---------- Perusta ---------- */
html {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  line-height: 1.5;
  color: var(--text);
  background-color: var(--bg);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
body { margin: 0; min-height: 100vh; display: flex; flex-direction: column; }

.skip-link {
  position: absolute;
  top: -40px;
  left: 0;
  padding: 8px 12px;
  background: var(--primary);
  color: white;
  text-decoration: none;
  border-radius: var(--radius);
  z-index: 9999;
  font-weight: 600;
}
.skip-link:focus { top: 0; }

/* ---------- Ylä- ja alatunniste ---------- */
.app-header {
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
  padding: 16px 24px; background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow-sm); position: sticky; top: 0; z-index: 100;
}
.app-header h1 { margin: 0; font-size: 1.25rem; font-weight: 600; color: var(--text); }
.header-summary { margin: 4px 0 0; font-size: 0.875rem; color: var(--text-muted); }

.app-footer {
  padding: 14px 24px; text-align: center;
  font-size: 0.8rem; color: var(--text-muted);
  border-top: 1px solid var(--border); background: var(--surface);
}
.app-footer p { margin: 0; }

main {
  flex: 1;
  display: flex; flex-direction: column; align-items: center;
  padding: 24px 16px; gap: 20px; position: relative;
}

/* ---------- Avustajan valikko ---------- */
.assistant-panel {
  position: absolute; top: 12px; right: 0;
  width: 340px; max-width: calc(100vw - 24px);
  max-height: calc(100vh - 80px);
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow-lg);
  padding: 16px; overflow-y: auto; z-index: 9999;
}
.assistant-panel[hidden] { display: none; }
.panel-group { margin-bottom: 16px; }
.panel-group h2 { margin: 0 0 12px; font-size: 1.125rem; font-weight: 600; }
.panel-group .panel-group { margin-top: 12px; }

.presets { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
.preset-btn {
  padding: 8px 12px; border: 1px solid var(--border); background: var(--surface);
  color: var(--text); border-radius: var(--radius); cursor: pointer;
  transition: all 0.2s ease; font-size: 0.875rem;
}
.preset-btn:hover, .preset-btn:focus-visible { border-color: var(--primary); background: var(--primary); color: white; outline: none; }
.preset-add { display: block; font-size: 0.875rem; font-weight: 500; }
.preset-add-row { display: flex; align-items: center; gap: 8px; margin-top: 6px; }
.preset-add-row input {
  width: 70px; padding: 6px 8px; border: 1px solid var(--border);
  border-radius: var(--radius); text-align: center; background: var(--surface); color: var(--text);
}

.choice-group { display: flex; gap: 16px; margin: 12px 0; border: 0; padding: 0; }
.choice-group legend { font-size: 0.8rem; font-weight: 600; color: var(--text-muted); padding: 0; }
.choice { display: flex; align-items: center; gap: 6px; cursor: pointer; font-size: 0.875rem; }
.choice input { margin: 0; accent-color: var(--primary); }

.switch-row, .range-row {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 0; border-bottom: 1px solid var(--border); font-size: 0.875rem;
}
.switch-row input { order: 2; margin: 0 0 0 12px; accent-color: var(--primary); flex-shrink: 0; }
.switch-row span { order: 1; text-align: left; }
.range-row { flex-wrap: wrap; }
.range-row input[type="range"] { flex: 1; min-width: 120px; margin: 0 12px; }
.range-row output { font-weight: 600; min-width: 64px; text-align: right; }

.hint { margin: 4px 0 0; font-size: 0.75rem; color: var(--text-muted); }

.timer-list { margin-top: 8px; }
.timer-thumb {
  display: flex; align-items: center; padding: 8px;
  border: 1px solid var(--border); border-radius: var(--radius);
  margin-bottom: 8px; cursor: pointer; transition: all 0.2s ease;
  background: var(--surface); color: var(--text);
}
.timer-thumb:hover { border-color: var(--primary); }
.timer-thumb.active { border-color: var(--primary); background: rgba(0, 102, 204, 0.05); }
.thumb-visual {
  width: 32px; height: 32px; border-radius: 50%; flex-shrink: 0;
  background: conic-gradient(var(--timer-circle-fill) 0 75%, var(--timer-circle-bg) 75% 100%);
  margin-right: 12px;
}
.thumb-visual.bar {
  border-radius: 4px;
  background: linear-gradient(to top, var(--timer-bar-fill) 0 75%, var(--timer-bar-bg) 75% 100%);
}
.thumb-time { font-size: 0.875rem; font-weight: 600; }
.panel-actions { display: flex; justify-content: space-between; margin-top: 16px; }
.panel-actions .btn { flex: 1; margin: 0 4px; }

/* ---------- Ajastimen näyttämö ---------- */
.timer-stage { display: flex; flex-direction: column; align-items: center; gap: 24px; width: 100%; }
.timer-wrap {
  position: relative;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 20px; padding: 8px;
}

.visual { position: relative; }

/* Ympyrätila: kello, jossa jäljellä oleva aika on väriä */
.mode-circle .visual {
  width: min(64vmin, 340px);
  height: min(64vmin, 340px);
  border-radius: 50%;
  background: conic-gradient(
    from 0deg,
    var(--timer-fill) 0deg,
    var(--timer-fill) calc(var(--remaining, 1) * 360deg),
    var(--timer-circle-bg) calc(var(--remaining, 1) * 360deg) 360deg
  );
  box-shadow: var(--shadow-md), inset 0 0 0 1px rgba(0, 0, 0, 0.06);
}

/* Pylvästila: lämpömittari, jossa jäljellä oleva aika näkyy pylväänä */
.mode-bar .visual {
  width: min(30vmin, 150px);
  height: min(60vmin, 320px);
  border-radius: 12px;
  background: var(--timer-bar-bg);
  box-shadow: var(--shadow-md), inset 0 0 0 1px rgba(0, 0, 0, 0.06);
  overflow: hidden;
}
.mode-bar .visual::before {
  content: '';
  position: absolute; left: 0; right: 0; bottom: 0;
  height: calc(var(--remaining, 1) * 100%);
  background: var(--timer-fill);
  transition: height 0.2s linear;
}

/* ---------- Asteikko (minuuttimerkinnät) ---------- */
/* Ympyrä: tick-elementti kattaa koko visualin ja sen reunaviiva on yläreunassa */
.mode-circle .tick {
  position: absolute; inset: 0;
  transform: rotate(var(--rot, 0deg));
  pointer-events: none;
}
.mode-circle .tick::before {
  content: '';
  position: absolute; top: 6px; left: 50%;
  width: 2px; height: 12px;
  transform: translateX(-50%);
  background: var(--tick-minor);
  border-radius: 1px;
}
.mode-circle .tick-major::before { width: 3px; height: 20px; background: var(--tick-major); }

/* Pylväs: vaakasuora viiva kohdassa --f (0 = ala, 1 = ylä) */
.mode-bar .tick {
  position: absolute; left: 0; right: 0; height: 0;
  top: calc((1 - var(--f, 0)) * 100%);
  border-top: 1px solid var(--tick-minor);
  pointer-events: none;
}
.mode-bar .tick-major { border-top-width: 2px; border-top-color: var(--tick-major); }

/* ---------- Lukema ---------- */
.mode-circle .timer-readout {
  position: absolute; top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  width: 58%; aspect-ratio: 1 / 1;
  border-radius: 50%;
  background: var(--surface);
  box-shadow: var(--shadow-md);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px; padding: 10px; text-align: center;
  pointer-events: none;
}
.mode-bar .timer-readout {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  text-align: center;
}
.readout {
  font-size: clamp(1.6rem, 7vmin, 2.8rem);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--text);
  letter-spacing: 0.02em;
}
.readout-caption { margin: 0; font-size: 0.75rem; color: var(--text-muted); }
.end-badge { margin: 4px 0 0; font-weight: 600; color: var(--danger); }

/* Numeroiden piilotus: piilottaa ajan, mutta jättää "Aika loppui" -viestin */
.timer-wrap.hide-numbers .readout,
.timer-wrap.hide-numbers .readout-caption { display: none; }

/* Vilkkuminen viimeisen minuutin aikana */
.pulse-end { animation: pulse-end 1.2s infinite alternate; }
@keyframes pulse-end { 0% { opacity: 1; } 100% { opacity: 0.4; } }
@media (prefers-reduced-motion: reduce) {
  .pulse-end { animation: none; }
  .mode-bar .visual::before { transition: none; }
}

/* ---------- Ohjaimet ---------- */
.controls { width: 100%; max-width: 340px; display: flex; flex-direction: column; gap: 12px; }
.quick-row { display: flex; align-items: center; gap: 12px; font-size: 0.9rem; }
.quick-row input[type=range] { flex: 1; accent-color: var(--primary); }
.quick-row output { min-width: 56px; text-align: right; font-weight: 600; }
.button-row { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }
.status { font-size: 0.85rem; color: var(--text-muted); text-align: center; min-height: 1.2em; margin: 0; }

/* ---------- Napit ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 8px 14px; border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); color: var(--text); cursor: pointer;
  font-size: 0.9rem; font-weight: 500; transition: all 0.2s ease;
}
.btn:hover:not(:disabled) { border-color: var(--primary); background: var(--primary-dark); color: #fff; }
.btn:focus-visible { outline: none; box-shadow: var(--focus-ring); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; border-color: var(--primary); }
.btn-primary:hover:not(:disabled) { background: var(--primary-dark); }
.btn-ghost { background: transparent; color: var(--text-muted); }
.btn-ghost:hover:not(:disabled) { background: rgba(0, 0, 0, 0.05); color: var(--text); }
[data-theme="dark"] .btn-ghost:hover:not(:disabled) { background: rgba(255, 255, 255, 0.08); }
.btn-small { padding: 6px 10px; font-size: 0.8rem; }
.btn-assistant { white-space: nowrap; }

/* ---------- Hyödylliset ---------- */
.visually-hidden {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* ---------- Tulostus ---------- */
@media print {
  .assistant-panel, .controls, footer, .app-header .btn { display: none !important; }
  .timer-stage { max-width: none; }
}

/* ---------- Kapeat näytöt ---------- */
@media (max-width: 480px) {
  .app-header { padding: 12px 16px; }
  .app-header h1 { font-size: 1.05rem; }
  .assistant-panel { top: 8px; right: 8px; width: min(340px, calc(100vw - 16px)); }
  main { padding: 16px 8px; }
}
