:root {
  --bg: #f4f5fb;
  --surface: #ffffff;
  --surface-2: #f9fafc;
  --border: #e5e7ef;
  --text: #1f2333;
  --text-muted: #6b7080;
  --primary: #4f46e5;
  --primary-dark: #4338ca;
  --primary-soft: #eef0ff;
  --danger: #e11d48;
  --success: #16a34a;
  --warning: #f59e0b;
  --shadow: 0 4px 20px rgba(31, 35, 51, 0.08);
  --radius: 14px;
  --radius-sm: 9px;
}

[data-theme="dark"] {
  --bg: #14161f;
  --surface: #1e2130;
  --surface-2: #262a3b;
  --border: #333749;
  --text: #e6e8f0;
  --text-muted: #9aa0b4;
  --primary: #7c83ff;
  --primary-dark: #6a72f0;
  --primary-soft: #2a2f4a;
  --danger: #fb5f7e;
  --success: #34d17a;
  --warning: #fbbf24;
  --shadow: 0 4px 20px rgba(0, 0, 0, 0.35);
}

html { transition: none; }
body { transition: background .2s, color .2s; }

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
}

button { font-family: inherit; cursor: pointer; }

.app {
  display: grid;
  grid-template-columns: 240px 1fr;
  height: 100vh;
  overflow: hidden;
}

/* ---------- Sivupalkki ---------- */
.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 18px 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
  font-size: 18px;
  padding: 6px 8px 16px;
}
.brand-icon { font-size: 22px; }

.nav { display: flex; flex-direction: column; gap: 4px; }

.nav-btn {
  display: flex;
  align-items: center;
  gap: 12px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 15px;
  font-weight: 600;
  padding: 11px 12px;
  border-radius: var(--radius-sm);
  text-align: left;
  transition: background .15s, color .15s;
}
.nav-btn:hover { background: var(--surface-2); color: var(--text); }
.nav-btn.active { background: var(--primary-soft); color: var(--primary); }
.nav-icon { font-size: 17px; }

.nav-badge {
  margin-left: auto;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: 20px;
  background: var(--danger);
  color: #fff;
  font-size: 12px;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.nav-badge[hidden] { display: none; }

.add-btn {
  margin-top: auto;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  background: var(--primary);
  color: #fff;
  border: none;
  font-size: 15px;
  font-weight: 700;
  padding: 12px;
  border-radius: var(--radius-sm);
  transition: background .15s, transform .1s;
}
.add-btn:hover { background: var(--primary-dark); }
.add-btn:active { transform: scale(.98); }
.add-btn span { font-size: 20px; line-height: 1; }

.wipe-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  background: transparent;
  color: var(--danger);
  border: 1px solid var(--border);
  font-size: 14px;
  font-weight: 600;
  padding: 10px;
  border-radius: var(--radius-sm);
  transition: background .15s, border-color .15s;
}
.wipe-btn:hover { background: #fdecef; border-color: var(--danger); }
.wipe-btn:active { transform: scale(.98); }
.wipe-btn span { font-size: 15px; line-height: 1; }

.sidebar-footer {
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.cog-btn {
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 20px;
  width: 40px;
  height: 40px;
  border-radius: var(--radius-sm);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background .15s, color .15s;
}
.cog-btn:hover { background: var(--surface-2); color: var(--text); }
.cog-btn.active { background: var(--primary-soft); color: var(--primary); }

.collapse-btn {
  font-size: 26px;
  width: 48px;
  height: 48px;
  background: var(--surface-2);
  color: var(--text);
  justify-content: center;
  text-align: center;
}
.collapse-btn:hover { background: var(--primary-soft); color: var(--primary); }

/* ---------- Kutistettu sivupalkki (vain kuvakkeet) ---------- */
.app.sidebar-collapsed { grid-template-columns: 72px 1fr; }
.app.sidebar-collapsed .sidebar { padding-left: 11px; padding-right: 10px; }
.app.sidebar-collapsed .brand { justify-content: center; padding: 6px 0 16px; }
.app.sidebar-collapsed .brand-text { display: none; }

/* Kaikki kutistetun palkin painikkeet samankokoisia ja keskitettyjä */
.app.sidebar-collapsed .nav-btn,
.app.sidebar-collapsed .add-btn,
.app.sidebar-collapsed .wipe-btn,
.app.sidebar-collapsed .cog-btn {
  width: 48px;
  height: 48px;
  margin-left: auto;
  margin-right: auto;
  padding: 0;
  gap: 0;
  justify-content: center;
}
.app.sidebar-collapsed .nav-btn {
  font-size: 0;
  position: relative;
}
.app.sidebar-collapsed .nav-icon { font-size:18px; }
.app.sidebar-collapsed .nav-badge {
  position: absolute;   
  top: 3px;
  right: 6px;
  margin: 0;
  min-width: 16px;
  height: 16px;
  font-size: 10px;
}
.app.sidebar-collapsed .add-btn { font-size: 0; }
.app.sidebar-collapsed .add-btn span { font-size: 22px; }
.app.sidebar-collapsed .wipe-btn { font-size: 0; }
.app.sidebar-collapsed .wipe-btn span { font-size: 16px; }
.app.sidebar-collapsed .sidebar-footer { flex-direction: column; gap: 8px; }

/* ---------- Pääsisältö ---------- */
.content { display: flex; flex-direction: column; overflow: hidden; }

.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px 24px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.page-title { font-size: 20px; margin: 0; }
.topbar-actions { margin-left: auto; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

.icon-btn {
  border: none;
  background: transparent;
  font-size: 18px;
  padding: 6px 10px;
  border-radius: 8px;
  color: var(--text);
}
.icon-btn:hover { background: var(--surface-2); }
.menu-toggle { display: none; }
.theme-toggle { margin-left: 8px; font-size: 18px; }

.view { padding: 24px; overflow-y: auto; flex: 1; }

/* ---------- Painikkeet ---------- */
.btn {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 14px;
  font-weight: 600;
  padding: 9px 16px;
  border-radius: var(--radius-sm);
  transition: background .15s, border-color .15s;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-dark); }
.btn.ghost { border-color: transparent; color: var(--danger); }
.btn.ghost:hover { background: #fdecef; }
.btn.small { padding: 6px 12px; font-size: 13px; }

/* ---------- Kalenteri ---------- */
.cal-toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}
.cal-title { font-size: 18px; font-weight: 700; min-width: 170px; text-transform: capitalize; }
.view-switch { display: flex; gap: 4px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 3px; }
.view-switch button {
  border: none; background: transparent; padding: 6px 14px;
  border-radius: 6px; font-weight: 600; font-size: 13px; color: var(--text-muted);
}
.view-switch button.active { background: var(--primary-soft); color: var(--primary); }

.cal-grid {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
}

/* Kalenterin asettelu: valitun päivän paneeli (vasen) + ruudukko (oikea) */
.cal-layout {
  display: grid;
  grid-template-columns: 300px minmax(0, 1fr);
  gap: 16px;
  align-items: start;
}
.day-detail {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px;
  position: sticky;
  top: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.day-detail-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.day-detail-weekday {
  font-size: 13px;
  color: var(--text-muted);
  font-weight: 600;
  text-transform: capitalize;
}
.day-detail-date { font-size: 20px; font-weight: 700; }
.day-add { align-self: stretch; }
.day-empty { color: var(--text-muted); font-size: 13px; padding: 10px 2px; }
.cal-cell.selected { box-shadow: inset 0 0 0 2px var(--primary); }

.cal-weekdays {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
}
.cal-weekdays span {
  padding: 10px;
  text-align: center;
  font-size: 12px;
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
}
.cal-days { display: grid; grid-template-columns: repeat(7, 1fr); }
.cal-cell {
  min-height: 108px;
  min-width: 0;
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  padding: 6px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  cursor: pointer;
  transition: background .12s;
}
.cal-cell:hover { background: var(--surface-2); }
.cal-cell:nth-child(7n) { border-right: none; }
.cal-cell.other-month { background: var(--surface-2); }
.cal-cell.other-month .cal-daynum { color: #c3c6d4; }
.cal-daynum { font-size: 13px; font-weight: 600; align-self: flex-start; padding: 2px 6px; border-radius: 6px; }
.cal-cell.today .cal-daynum { background: var(--primary); color: #fff; }

.cal-event {
  font-size: 11.5px;
  font-weight: 600;
  padding: 3px 6px;
  border-radius: 5px;
  color: #fff;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  border-left: 3px solid rgba(0,0,0,.2);
}
.cal-event.done { opacity: .5; text-decoration: line-through; }
.cal-more { font-size: 11px; color: var(--text-muted); font-weight: 600; }

/* Viikkonäkymä */
.week-list { display: flex; flex-direction: column; gap: 14px; }
.week-day {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.week-day-header {
  padding: 10px 16px;
  background: var(--surface-2);
  font-weight: 700;
  font-size: 14px;
  text-transform: capitalize;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
}
.week-day-header.today { background: var(--primary-soft); color: var(--primary); }
.week-day-body { padding: 8px; display: flex; flex-direction: column; gap: 6px; }
.week-empty { padding: 14px 16px; color: var(--text-muted); font-size: 13px; }

/* ---------- Tehtäväkortit ---------- */
.task-card {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 4px solid var(--primary);
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  transition: box-shadow .15s;
}
.task-card:hover { box-shadow: var(--shadow); }
.task-check {
  width: 22px; height: 22px; border-radius: 6px; border: 2px solid var(--border);
  background: var(--surface); flex-shrink: 0; display: grid; place-items: center;
  font-size: 14px; color: #fff;
}
.task-check.done { background: var(--success); border-color: var(--success); }
.task-main { flex: 1; min-width: 0; cursor: pointer; }
.task-title { font-weight: 600; font-size: 14.5px; }
.task-title.done { text-decoration: line-through; color: var(--text-muted); }
.task-meta { font-size: 12.5px; color: var(--text-muted); display: flex; gap: 10px; flex-wrap: wrap; margin-top: 2px; }
.task-course-dot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; }
.badge {
  font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 20px; white-space: nowrap;
}
.badge.high { background: #fde2e6; color: var(--danger); }
.badge.normal { background: #e7edff; color: var(--primary); }
.badge.low { background: #e9eef2; color: var(--text-muted); }
.badge.doing { background: #fff2d9; color: #b45309; }
.badge.overdue { background: var(--danger); color: #fff; }
.badge.today { background: var(--warning); color: #fff; }

/* ---------- Tehtävälista ---------- */
.list-controls { display: flex; gap: 10px; margin-bottom: 16px; flex-wrap: wrap; }
.list-controls select, .list-controls input {
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 8px 12px; font-size: 14px; background: var(--surface); color: var(--text);
}
.task-group { margin-bottom: 22px; }
.task-group-title { font-size: 14px; font-weight: 700; color: var(--text-muted); margin-bottom: 8px; text-transform: uppercase; letter-spacing: .03em; }
.task-list { display: flex; flex-direction: column; gap: 8px; }

/* ---------- Kurssit ---------- */
.courses-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 14px; }
.course-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; box-shadow: var(--shadow); cursor: pointer; transition: transform .1s;
  border-top: 5px solid var(--primary);
}
.course-card:hover { transform: translateY(-2px); }
.course-name { font-weight: 700; font-size: 16px; margin-bottom: 4px; }
.course-term { font-size: 13px; color: var(--text-muted); }
.course-count { font-size: 13px; color: var(--text-muted); margin-top: 10px; }

/* ---------- Modaali ---------- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(20, 22, 34, .45);
  display: grid; place-items: center; padding: 16px; z-index: 50;
  animation: fade .15s ease;
}
.modal-overlay[hidden] { display: none; }
.toast[hidden] { display: none; }
.modal {
  background: var(--surface); border-radius: var(--radius); width: 100%; max-width: 480px;
  max-height: 92vh; overflow-y: auto; box-shadow: 0 20px 60px rgba(0,0,0,.25);
  animation: pop .15s ease;
}
.modal.small { max-width: 400px; }
.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 18px 20px; border-bottom: 1px solid var(--border);
}
.modal-header h2 { margin: 0; font-size: 18px; }
.modal-body { padding: 20px; display: flex; flex-direction: column; gap: 14px; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; font-weight: 600; color: var(--text-muted); }
.field input, .field select, .field textarea {
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 10px 12px; font-size: 14px; color: var(--text); background: var(--surface);
  font-family: inherit; font-weight: 500;
}
.field input:focus, .field select:focus, .field textarea:focus {
  outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft);
}
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.modal-footer { display: flex; align-items: center; gap: 8px; padding-top: 8px; }
.spacer { flex: 1; }

.color-picker { display: flex; flex-wrap: wrap; gap: 8px; }
.color-swatch {
  width: 30px; height: 30px; border-radius: 50%; cursor: pointer;
  border: 3px solid transparent; transition: transform .1s;
}
.color-swatch:hover { transform: scale(1.12); }
.color-swatch.selected { border-color: var(--text); }

/* ---------- Asetukset ---------- */
.settings-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 20px; box-shadow: var(--shadow); max-width: 560px; margin-bottom: 16px;
}
.settings-card h3 { margin: 0 0 6px; font-size: 16px; }
.settings-card p { margin: 0 0 14px; color: var(--text-muted); font-size: 14px; }
.settings-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 0; border-top: 1px solid var(--border); }
.backup-text {
  width: 100%; margin: 8px 0; padding: 10px; resize: vertical;
  font-family: monospace; font-size: 12px; word-break: break-all;
  background: var(--bg); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px;
}

/* ---------- Tyhjä tila ---------- */
.empty-state { text-align: center; padding: 60px 20px; color: var(--text-muted); }
.empty-state .emoji { font-size: 48px; display: block; margin-bottom: 12px; }
.empty-actions { margin-top: 18px; }

/* ---------- Tervetulo-esittely ---------- */
.welcome-card {
  position: relative;
  margin-bottom: 18px;
  padding: 20px 22px;
  border: 1px solid var(--border);
  border-left: 4px solid var(--primary, #4f46e5);
  border-radius: 12px;
  background: var(--surface, #fff);
  box-shadow: var(--shadow);
}
.welcome-close {
  position: absolute; top: 10px; right: 12px;
  border: none; background: transparent; cursor: pointer;
  font-size: 16px; line-height: 1; color: var(--text-muted);
  padding: 4px; border-radius: 6px;
}
.welcome-close:hover { background: var(--bg); color: var(--text); }
.welcome-title { font-size: 18px; font-weight: 700; margin-bottom: 6px; padding-right: 24px; }
.welcome-lead { margin: 0 0 12px; color: var(--text-muted); font-size: 14px; }
.welcome-steps { margin: 0 0 16px; padding-left: 20px; display: grid; gap: 6px; font-size: 14px; }
.welcome-steps li { line-height: 1.4; }
.welcome-actions { display: flex; flex-wrap: wrap; gap: 10px; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--text); color: #fff; padding: 12px 22px; border-radius: 30px;
  font-size: 14px; font-weight: 600; box-shadow: var(--shadow); z-index: 100;
  animation: slideUp .2s ease;
}

@keyframes fade { from { opacity: 0; } }
@keyframes pop { from { opacity: 0; transform: scale(.96); } }
@keyframes slideUp { from { opacity: 0; transform: translate(-50%, 12px); } }

/* ---------- Responsiivisuus ---------- */
@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; width: 240px; z-index: 40;
    transform: translateX(-100%); transition: transform .2s; box-shadow: var(--shadow);
  }
  .sidebar.open { transform: translateX(0); }
  .menu-toggle { display: inline-flex; }
  .cal-cell { min-height: 74px; }
  .cal-layout { grid-template-columns: 1fr; }
  .day-detail { position: static; }
  .collapse-btn { display: none; }
  .app.sidebar-collapsed { grid-template-columns: 1fr; }
  .cal-event { font-size: 0; padding: 4px; }
  .cal-event::before { content: ""; display: block; width: 6px; height: 6px; border-radius: 50%; background: #fff; }
  .field-row { grid-template-columns: 1fr; }
}
