/* Class Hub — styles. Tokens and palette from docs/design-spec.md §1–2 (ใบเฟิร์น). Mobile-first. */

/* =========================================================================
   1. Design tokens
   ========================================================================= */
:root {
  color-scheme: light dark;

  --font-sans: system-ui, -apple-system, "Segoe UI", "Leelawadee UI", "Thonburi",
               "Noto Sans Thai", "Sarabun", Tahoma, sans-serif;
  --font-mono: ui-monospace, "Cascadia Mono", Consolas, monospace;

  --fs-xs: 0.75rem;
  --fs-sm: 0.875rem;
  --fs-md: 1rem;
  --fs-lg: 1.125rem;
  --fs-xl: 1.375rem;
  --fs-2xl: 1.75rem;
  --lh-tight: 1.35;
  --lh-body: 1.6;
  --fw-regular: 400;
  --fw-semibold: 600;
  --fw-bold: 700;

  --sp-1: 4px;  --sp-2: 8px;  --sp-3: 12px; --sp-4: 16px;
  --sp-5: 24px; --sp-6: 32px; --sp-7: 48px;

  --r-sm: 6px;
  --r-md: 10px;
  --r-lg: 14px;
  --r-full: 999px;

  --container: 1200px;
  --header-h: 56px;
  --tap: 44px;
  --hour-h: 64px;
  --accent-w: 4px;

  --bg: #F6F7FB;
  --surface: #FFFFFF;
  --surface-2: #EEF0F5;
  --text: #1F2328;
  --text-muted: #5B6270;
  --border: #E1E4EA;
  --border-strong: #7B8190;

  --primary: #4F5BD5;
  --primary-hover: #3F4AB8;
  --on-primary: #FFFFFF;
  --primary-soft: #EEF0FF;

  --danger: #C2353A;
  --danger-hover: #A32028;
  --on-danger: #FFFFFF;
  --danger-text: #B42318;
  --danger-soft: #FDECEC;
  --danger-soft-text: #B42318;

  --warning-text: #8A4B00;
  --warning-soft: #FFF4E0;
  --success-text: #1C5430;
  --success-soft: #E3F5E8;

  --focus: #4F5BD5;
  --backdrop: rgb(20 22 28 / 0.5);

  --shadow-sm: 0 1px 2px rgb(16 24 40 / 0.06), 0 1px 3px rgb(16 24 40 / 0.10);
  --shadow-md: 0 4px 12px rgb(16 24 40 / 0.10);
  --shadow-lg: 0 12px 32px rgb(16 24 40 / 0.18);

  --dur: 150ms;

  /* layout helpers (not in spec token list; derived from tokens) */
  --week-head-h: 44px;
  --axis-w: 56px;
  --hover-filter: brightness(0.97);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161C;
    --surface: #1E2129;
    --surface-2: #2A2E38;
    --text: #E8EAF0;
    --text-muted: #A3AAB8;
    --border: #343844;
    --border-strong: #6B7385;

    --primary: #9AA5FF;
    --primary-hover: #B3BBFF;
    --on-primary: #14161C;
    --primary-soft: #262B4A;

    --danger: #FF8A80;
    --danger-hover: #FFA59D;
    --on-danger: #14161C;
    --danger-text: #FF8A80;
    --danger-soft: #3D1F1F;
    --danger-soft-text: #FFB4AB;

    --warning-text: #FFD58A;
    --warning-soft: #3A2E14;
    --success-text: #9BE0B0;
    --success-soft: #18301F;

    --focus: #9AA5FF;
    --backdrop: rgb(0 0 0 / 0.6);

    --shadow-sm: 0 1px 2px rgb(0 0 0 / 0.4);
    --shadow-md: 0 4px 12px rgb(0 0 0 / 0.45);
    --shadow-lg: 0 12px 32px rgb(0 0 0 / 0.6);

    --hover-filter: brightness(1.1);
  }
}

@media (prefers-reduced-motion: reduce) {
  :root { --dur: 0ms; }
  * { scroll-behavior: auto !important; }
}

/* =========================================================================
   2. Subject palette (12 colours, §2.2). Element sets data-color="<key>".
   ========================================================================= */
[data-color="yellow"]     { --s-bg: #FFF3C4; --s-accent: #E0B000; --s-text: #5C4300; }
[data-color="pink"]       { --s-bg: #FFE1EC; --s-accent: #E46A9A; --s-text: #7A1E46; }
[data-color="green"]      { --s-bg: #DDF4E2; --s-accent: #46A866; --s-text: #1C5430; }
[data-color="sky"]        { --s-bg: #DCEDFF; --s-accent: #4A97DC; --s-text: #18476F; }
[data-color="lavender"]   { --s-bg: #ECE4FF; --s-accent: #8C6BDE; --s-text: #43267D; }
[data-color="peach"]      { --s-bg: #FFE5D4; --s-accent: #EE874A; --s-text: #76330E; }
[data-color="mint"]       { --s-bg: #D5F4EE; --s-accent: #2AAE9B; --s-text: #0F5148; }
[data-color="rose"]       { --s-bg: #FFE0E0; --s-accent: #DD6464; --s-text: #7B1D1D; }
[data-color="lime"]       { --s-bg: #EDF6D0; --s-accent: #89AD33; --s-text: #3B4F12; }
[data-color="periwinkle"] { --s-bg: #E1E6FF; --s-accent: #6375D6; --s-text: #29357E; }
[data-color="sand"]       { --s-bg: #F3EADB; --s-accent: #B08752; --s-text: #573F20; }
[data-color="aqua"]       { --s-bg: #D6F2FA; --s-accent: #26A5C6; --s-text: #0D4C5C; }

@media (prefers-color-scheme: dark) {
  [data-color="yellow"]     { --s-bg: #3A3212; --s-accent: #E8C547; --s-text: #FFE9A3; }
  [data-color="pink"]       { --s-bg: #3D1F2B; --s-accent: #F08DB4; --s-text: #FFD0E2; }
  [data-color="green"]      { --s-bg: #1C3324; --s-accent: #6FCB8C; --s-text: #C6F0D2; }
  [data-color="sky"]        { --s-bg: #1A2B3D; --s-accent: #72B4EE; --s-text: #CDE6FF; }
  [data-color="lavender"]   { --s-bg: #2A2240; --s-accent: #AE93F0; --s-text: #E2D6FF; }
  [data-color="peach"]      { --s-bg: #3D2619; --s-accent: #F4A271; --s-text: #FFDCC6; }
  [data-color="mint"]       { --s-bg: #15332F; --s-accent: #5ACDBB; --s-text: #C2F0E8; }
  [data-color="rose"]       { --s-bg: #3E1F21; --s-accent: #EE8A8A; --s-text: #FFD3D3; }
  [data-color="lime"]       { --s-bg: #272F14; --s-accent: #AFD158; --s-text: #E6F5BE; }
  [data-color="periwinkle"] { --s-bg: #1F2440; --s-accent: #8D9CF0; --s-text: #D7DDFF; }
  [data-color="sand"]       { --s-bg: #332A1F; --s-accent: #D0A877; --s-text: #F2E3CC; }
  [data-color="aqua"]       { --s-bg: #14303A; --s-accent: #56C4E0; --s-text: #C4EDF8; }
}

.subject-surface {
  background: var(--s-bg);
  color: var(--s-text);
  border-left: var(--accent-w) solid var(--s-accent);
}

/* =========================================================================
   3. Base
   ========================================================================= */
*, *::before, *::after { box-sizing: border-box; }

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

body {
  margin: 0;
  font-family: var(--font-sans);
  font-size: var(--fs-md);
  line-height: var(--lh-body);
  color: var(--text);
  background: var(--bg);
  overflow-x: hidden;
}

h1, h2, h3, h4 { line-height: var(--lh-tight); margin: 0; }
p { margin: 0; }
ul, ol { margin: 0; padding: 0; }
dialog { color: var(--text); }
[hidden] { display: none !important; }

a { color: var(--primary); }
a:hover { color: var(--primary-hover); }

:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}
[tabindex="-1"]:focus:not(:focus-visible) { outline: none; }

.icon { flex: none; width: 18px; height: 18px; }
.shape { flex: none; width: 8px; height: 8px; }

.visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px;
  margin: -1px; padding: 0; border: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
}

.skip-link {
  position: absolute;
  left: var(--sp-2);
  top: -100px;
  z-index: 100;
  padding: var(--sp-2) var(--sp-4);
  background: var(--surface);
  color: var(--primary);
  border-radius: var(--r-md);
  box-shadow: var(--shadow-md);
  font-weight: var(--fw-semibold);
}
.skip-link:focus { top: var(--sp-2); }

.plain-list { list-style: none; }

.noscript { padding: var(--sp-5); }

time, .task-meta, .cal-daynum, .status-badge { font-variant-numeric: tabular-nums; }

/* =========================================================================
   4. Header / nav / layout
   ========================================================================= */
.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  height: var(--header-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.app-header-inner {
  max-width: var(--container);
  height: 100%;
  margin: 0 auto;
  padding: 0 var(--sp-4);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--sp-3);
}
.logo {
  font-size: var(--fs-lg);
  font-weight: var(--fw-bold);
  color: var(--text);
  text-decoration: none;
  white-space: nowrap;
}
.logo:hover { color: var(--primary); }
.app-nav { display: flex; gap: var(--sp-1); height: 100%; align-items: stretch; }
.nav-tab {
  display: inline-flex;
  align-items: center;
  min-height: var(--tap);
  align-self: center;
  padding: 0 var(--sp-3);
  border-radius: var(--r-md) var(--r-md) 0 0;
  color: var(--text-muted);
  text-decoration: none;
  font-weight: var(--fw-semibold);
  border-bottom: 3px solid transparent;
  transition: background var(--dur), color var(--dur);
}
.nav-tab:hover { background: var(--surface-2); color: var(--text); }
.nav-tab[aria-current="page"] {
  background: var(--primary-soft);
  color: var(--primary);
  font-weight: var(--fw-bold);
  border-bottom-color: var(--primary);
}

.container {
  max-width: var(--container);
  margin: 0 auto;
  padding: var(--sp-4);
  outline: none;
}
.view { display: flex; flex-direction: column; gap: var(--sp-5); min-width: 0; }

.panel {
  background: var(--surface);
  border-radius: var(--r-md);
  padding: var(--sp-4);
  min-width: 0;
}

.section-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--sp-2) var(--sp-3);
  margin-bottom: var(--sp-4);
}
.section-title { font-size: var(--fs-xl); font-weight: var(--fw-bold); outline-offset: 4px; }
.page-title { font-size: var(--fs-2xl); font-weight: var(--fw-bold); }
.panel > .section-title { margin-bottom: var(--sp-4); }

/* banner (load issue / save failure) */
.banner-area { max-width: var(--container); margin: 0 auto; padding: 0 var(--sp-4); }
.banner-area:empty { display: none; }
.banner {
  display: flex;
  align-items: flex-start;
  gap: var(--sp-3);
  margin-top: var(--sp-3);
  padding: var(--sp-3) var(--sp-4);
  background: var(--danger-soft);
  color: var(--danger-soft-text);
  border: 1px solid var(--danger-text);
  border-radius: var(--r-md);
}
.banner > .icon { margin-top: 4px; }
.banner-text { flex: 1; min-width: 0; }
.banner .btn-icon { color: var(--danger-soft-text); }

/* toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: var(--sp-4);
  z-index: 50;
  transform: translate(-50%, 20px);
  width: max-content;
  max-width: calc(100vw - 32px);
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  padding: var(--sp-3) var(--sp-4);
  background: var(--text);
  color: var(--surface);
  border-radius: var(--r-md);
  box-shadow: var(--shadow-lg);
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--dur), transform var(--dur);
}
.toast.is-visible { opacity: 1; transform: translate(-50%, 0); }
.toast:empty { padding: 0; box-shadow: none; }

/* =========================================================================
   5. Buttons
   ========================================================================= */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--sp-2);
  min-height: var(--tap);
  padding: 0 var(--sp-4);
  border: 1px solid transparent;
  border-radius: var(--r-md);
  font: inherit;
  font-size: var(--fs-md);
  font-weight: var(--fw-semibold);
  line-height: 1.2;
  text-decoration: none;
  cursor: pointer;
  white-space: nowrap;
  transition: background var(--dur), color var(--dur), border-color var(--dur), transform var(--dur);
}
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-primary { background: var(--primary); color: var(--on-primary); }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); color: var(--on-primary); }

.btn-secondary { background: var(--surface); color: var(--text); border-color: var(--border-strong); }
.btn-secondary:hover:not(:disabled) { background: var(--surface-2); color: var(--text); }

.btn-danger { background: var(--danger); color: var(--on-danger); }
.btn-danger:hover:not(:disabled) { background: var(--danger-hover); color: var(--on-danger); }

.btn-danger-ghost { background: transparent; color: var(--danger-text); border-color: var(--danger-text); }
.btn-danger-ghost:hover:not(:disabled) { background: var(--danger-soft); color: var(--danger-text); }

.btn-ghost { background: transparent; color: var(--primary); }
.btn-ghost:hover:not(:disabled) { background: var(--surface-2); color: var(--primary-hover); }

.btn-icon { width: var(--tap); padding: 0; }
.btn-sm { font-size: var(--fs-sm); padding: 0 var(--sp-3); }
.btn-sm.btn-icon { padding: 0; }

/* Buttons on a subject-coloured surface: white surface, subject text colour (§5.3) */
.subject-surface .btn.on-surface {
  background: var(--surface);
  color: var(--s-text);
  border-color: var(--s-text);
}
.subject-surface .btn.on-surface:hover { background: var(--surface-2); color: var(--s-text); }
.subject-surface .btn-danger-ghost { background: var(--surface); }
.subject-surface .btn-danger-ghost:hover { background: var(--danger-soft); }

/* =========================================================================
   6. Subject chips (§5.1)
   ========================================================================= */
.chip-scroller {
  position: relative;
  margin: 0 calc(var(--sp-4) * -1);
}
.chip-scroller::after { /* fade hints that the row scrolls (mobile) */
  content: "";
  position: absolute;
  top: 0; right: 0; bottom: 0;
  width: var(--sp-5);
  background: linear-gradient(to right, transparent, var(--surface));
  pointer-events: none;
}
.subject-chips {
  list-style: none;
  display: flex;
  gap: var(--sp-2);
  overflow-x: auto;
  scroll-snap-type: x proximity;
  padding: var(--sp-1) var(--sp-5) var(--sp-2) var(--sp-4);
}
.subject-chips > li { flex: none; scroll-snap-align: start; }
.subject-chip {
  display: flex;
  flex-direction: column;
  justify-content: center;
  width: 168px;
  min-height: 56px;
  padding: var(--sp-2) var(--sp-3);
  border-radius: var(--r-md);
  text-decoration: none;
  transition: box-shadow var(--dur), transform var(--dur);
}
a.subject-chip:hover { color: var(--s-text); box-shadow: var(--shadow-md); transform: translateY(-1px); }
a.subject-chip:active { transform: none; }
.chip-code { font-size: var(--fs-sm); font-weight: var(--fw-bold); }
.chip-name, .chip-note {
  font-size: var(--fs-sm);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* =========================================================================
   7. Weekly schedule (§3.3, §4.1) — list per day on mobile, time grid ≥768
   ========================================================================= */
.week-axis { display: none; }
.week-day { padding: var(--sp-3) 0; border-top: 1px solid var(--border); }
.week-day:first-of-type { border-top: 0; padding-top: 0; }
.week-day.is-today { border-left: 3px solid var(--primary); padding-left: var(--sp-3); }
.week-day-head {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  font-size: var(--fs-md);
  font-weight: var(--fw-bold);
  margin-bottom: var(--sp-2);
}
.day-short { display: none; }
.today-badge {
  display: inline-block;
  padding: 0 var(--sp-2);
  border-radius: var(--r-sm);
  background: var(--primary-soft);
  color: var(--primary);
  font-size: var(--fs-sm);
  font-weight: var(--fw-semibold);
}
.week-slots { list-style: none; display: flex; flex-direction: column; gap: var(--sp-2); }
.week-empty { color: var(--text-muted); font-size: var(--fs-sm); }

.class-block {
  display: grid;
  grid-template-columns: auto 1fr;
  column-gap: var(--sp-3);
  padding: var(--sp-2) var(--sp-3);
  border-radius: var(--r-sm);
  text-decoration: none;
  overflow: hidden;
  font-size: var(--fs-sm);
  transition: filter var(--dur), box-shadow var(--dur);
}
.class-block:hover { color: var(--s-text); filter: var(--hover-filter); box-shadow: var(--shadow-sm); }
.cb-code { font-weight: var(--fw-bold); }
.cb-time { justify-self: start; }
.cb-name, .cb-room { grid-column: 1 / -1; overflow-wrap: anywhere; }

.week-note {
  display: flex;
  align-items: flex-start;
  gap: var(--sp-2);
  margin-top: var(--sp-4);
  color: var(--text-muted);
  font-size: var(--fs-sm);
}
.week-note .icon { margin-top: 3px; }

/* =========================================================================
   8. Task board + cards (§5.3)
   ========================================================================= */
.board { display: flex; flex-direction: column; gap: var(--sp-5); }
.board-col { min-width: 0; }
.board-title { font-size: var(--fs-lg); font-weight: var(--fw-bold); margin-bottom: var(--sp-3); outline-offset: 4px; }
.board-summary {
  list-style: none;
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  min-height: var(--tap);
  cursor: pointer;
  margin-bottom: var(--sp-2);
}
.board-summary::-webkit-details-marker { display: none; }
.board-summary::before {
  content: "";
  flex: none;
  width: 8px; height: 8px;
  border-right: 2px solid currentColor;
  border-bottom: 2px solid currentColor;
  transform: rotate(-45deg);
  transition: transform var(--dur);
}
details[open] > .board-summary::before { transform: rotate(45deg); }
.board-summary .board-title { margin-bottom: 0; }
.board-done { border-top: 1px solid var(--border); padding-top: var(--sp-3); }

.task-list { list-style: none; display: flex; flex-direction: column; gap: var(--sp-3); }
.task-grid { display: grid; grid-template-columns: minmax(0, 1fr); gap: var(--sp-3); }

.task-card {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: var(--sp-1);
  padding: var(--sp-3);
  border-radius: var(--r-md);
  min-width: 0;
  transition: box-shadow var(--dur);
}
.task-card:hover { box-shadow: var(--shadow-sm); }
.task-card.is-overdue { border: 1px solid var(--danger-text); border-left: var(--accent-w) solid var(--s-accent); }
.task-top { display: flex; align-items: flex-start; justify-content: space-between; gap: var(--sp-2); }
.task-subject {
  min-width: 0;
  font-size: var(--fs-sm);
  font-weight: var(--fw-semibold);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.task-title {
  font-size: var(--fs-lg);
  font-weight: var(--fw-bold);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  overflow-wrap: anywhere;
}
.task-title a { color: inherit; text-decoration: none; }
.task-title a:hover { text-decoration: underline; }
.stretched-link::after { content: ""; position: absolute; inset: 0; border-radius: var(--r-md); }
.stretched-link:focus-visible { outline: none; }
.stretched-link:focus-visible::after { outline: 3px solid var(--focus); outline-offset: 2px; }
.is-done .task-title a { text-decoration: line-through; }

.task-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0 var(--sp-1);
  font-size: var(--fs-sm);
  overflow-wrap: anywhere;
  min-width: 0;
}
.task-meta .icon { width: 16px; height: 16px; }
.task-meta > span { min-width: 0; }
.task-actions {
  position: relative;
  z-index: 1;
  display: flex;
  flex-wrap: wrap;
  gap: var(--sp-2);
  margin-top: var(--sp-2);
}

.status-badge, .type-badge {
  flex: none;
  display: inline-flex;
  align-items: center;
  gap: var(--sp-1);
  padding: 2px var(--sp-2);
  border-radius: var(--r-sm);
  font-size: var(--fs-sm);
  font-weight: var(--fw-semibold);
  line-height: 1.4;
  white-space: nowrap;
}
.status-badge .icon { width: 14px; height: 14px; }
.status-near { background: var(--warning-soft); color: var(--warning-text); }
.status-overdue { background: var(--danger-soft); color: var(--danger-soft-text); }
.status-done { background: var(--success-soft); color: var(--success-text); }
.type-badge { background: var(--surface); color: var(--s-text, var(--text)); border: 1px solid var(--s-text, var(--border-strong)); }

/* =========================================================================
   9. Exam item (§5.4)
   ========================================================================= */
.exam-item { display: flex; flex-direction: column; gap: var(--sp-1); min-width: 0; }
.exam-item.subject-surface { padding: var(--sp-3); border-radius: var(--r-md); }
.exam-top { display: flex; flex-wrap: wrap; align-items: center; gap: var(--sp-2); font-size: var(--fs-sm); }
.exam-subject { color: inherit; font-weight: var(--fw-semibold); overflow-wrap: anywhere; }
.exam-subject:hover { color: inherit; }
.exam-note { font-size: var(--fs-sm); overflow-wrap: anywhere; }
.exam-actions { margin-top: var(--sp-1); }
.exam-item:not(.subject-surface) .btn-ghost { padding-left: 0; }

/* =========================================================================
   10. Subject detail (§3.5)
   ========================================================================= */
.back-link {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-1);
  min-height: var(--tap);
  align-self: flex-start;
  font-weight: var(--fw-semibold);
  text-decoration: none;
}
.back-link:hover { text-decoration: underline; }
.view-subject { gap: var(--sp-4); }
.subject-hero {
  border-left-width: 8px;
  border-radius: var(--r-lg);
  padding: var(--sp-4);
  display: flex;
  flex-direction: column;
  gap: var(--sp-1);
}
.hero-top { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: var(--sp-2); }
.hero-code { font-weight: var(--fw-semibold); }
.hero-title { font-size: var(--fs-2xl); font-weight: var(--fw-bold); overflow-wrap: anywhere; outline-offset: 4px; }
.hero-sub { overflow-wrap: anywhere; }

.detail-grid { display: grid; grid-template-columns: minmax(0, 1fr); gap: var(--sp-4); }

.info-list { margin: 0; display: grid; grid-template-columns: minmax(0, 1fr); gap: var(--sp-1) var(--sp-4); }
.info-list dt { color: var(--text-muted); font-size: var(--fs-sm); font-weight: var(--fw-semibold); }
.info-list dd { margin: 0 0 var(--sp-3); overflow-wrap: anywhere; min-width: 0; }

.link-list { list-style: none; display: flex; flex-direction: column; margin-bottom: var(--sp-4); }
.link-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 0 var(--sp-2);
  padding: var(--sp-3) 0;
  border-bottom: 1px solid var(--border);
}
.link-item:first-child { padding-top: 0; }
.link-label { grid-column: 1; font-weight: var(--fw-semibold); overflow-wrap: anywhere; }
.link-value { grid-column: 1; overflow-wrap: anywhere; min-width: 0; }
a.link-value { display: inline; }
a.link-value .icon { width: 14px; height: 14px; vertical-align: -2px; margin-left: 2px; }
.link-actions { grid-column: 2; grid-row: 1 / span 2; display: flex; gap: var(--sp-1); align-items: flex-start; }
.link-item > .link-form { grid-column: 1 / -1; }
.link-form {
  display: flex;
  flex-direction: column;
  gap: var(--sp-3);
  padding: var(--sp-3);
  margin-bottom: var(--sp-3);
  background: var(--surface-2);
  border-radius: var(--r-md);
}
.form-actions { display: flex; justify-content: flex-end; gap: var(--sp-2); flex-wrap: wrap; }

.subject-done { margin-top: var(--sp-4); border-top: 1px solid var(--border); padding-top: var(--sp-3); }

/* =========================================================================
   11. Empty states (§5.6)
   ========================================================================= */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--sp-2);
  padding: var(--sp-5);
  text-align: center;
  border: 1px dashed var(--border-strong);
  border-radius: var(--r-md);
}
.empty-icon { width: 24px; height: 24px; color: var(--text-muted); }
.empty-title { font-weight: var(--fw-semibold); }
.empty-text { color: var(--text-muted); font-size: var(--fs-sm); }
.empty-state .btn { margin-top: var(--sp-2); }
.link-form + .btn, .empty-state + .btn, .link-list + .btn { margin-top: var(--sp-3); }

/* =========================================================================
   12. Calendar (§3.4, §5.5)
   ========================================================================= */
.view-calendar { gap: var(--sp-4); }
.cal-panel { display: flex; flex-direction: column; gap: var(--sp-3); }
.cal-toolbar {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: var(--sp-2);
}
.cal-toolbar .btn-ghost { padding: 0 var(--sp-2); }
.cal-month { min-width: 0; text-align: center; font-size: var(--fs-xl); font-weight: var(--fw-bold); }
/* BUG-07: month and year may go on separate lines, but a Thai month name is never cut mid-word */
.cal-month-part { white-space: nowrap; }
/* BUG-01 / BUG-07: below 480px the prev/next labels squeeze the month name (needs ~420px at default text size) — keep them for screen readers,
   show arrows only (44px touch targets) */
@media (max-width: 479.98px) {
  .cal-toolbar .cal-nav-label {
    position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0;
    overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
  }
  .cal-toolbar .btn-ghost { min-width: var(--tap); min-height: var(--tap); justify-content: center; }
  .cal-month { font-size: var(--fs-lg); }
}
.cal-toolbar > .btn-secondary { grid-column: 1 / -1; justify-self: center; }
.cal-legend { display: flex; flex-wrap: wrap; gap: var(--sp-1) var(--sp-4); font-size: var(--fs-sm); color: var(--text-muted); }
.legend-title { font-weight: var(--fw-semibold); }
.legend-item { display: inline-flex; align-items: center; gap: var(--sp-1); }
.legend-item .shape { color: var(--text); }

.cal-layout { display: grid; grid-template-columns: minmax(0, 1fr); gap: var(--sp-5); }
.cal-grid-wrap { min-width: 0; }
.cal-grid { width: 100%; table-layout: fixed; border-collapse: collapse; }
.cal-grid th {
  padding: var(--sp-1) 0;
  font-size: var(--fs-sm);
  font-weight: var(--fw-semibold);
  color: var(--text-muted);
}
.cal-cell { padding: 1px; vertical-align: top; height: 48px; }
.cal-cell.is-outside {
  background: var(--surface-2);
  color: var(--text-muted);
  text-align: center;
  font-size: var(--fs-sm);
}
.cal-cell.is-outside .cal-daynum { display: inline-block; padding-top: var(--sp-1); }
.cal-day {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  width: 100%;
  height: 100%;
  min-height: var(--tap);
  padding: var(--sp-1) 2px;
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: var(--fs-sm);
  cursor: pointer;
  overflow: hidden;
  transition: background var(--dur);
}
.cal-day:hover { background: var(--surface-2); }
.cal-day.is-selected { border: 2px solid var(--primary); background: var(--primary-soft); }
.cal-daynum {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 24px;
  height: 24px;
  border-radius: var(--r-full);
  font-weight: var(--fw-semibold);
}
.cal-day.is-today .cal-daynum { background: var(--primary); color: var(--on-primary); }
.cal-pills { display: flex; flex-wrap: wrap; justify-content: center; gap: 2px; width: 100%; min-width: 0; }

/* Mobile: pills collapse to shape dots in the subject accent colour */
.cal-pill { display: inline-flex; align-items: center; background: none; border: 0; padding: 0; color: var(--s-accent); }
.cal-pill .shape { width: 8px; height: 8px; }
.pill-full, .pill-short { display: none; }
.cal-more { font-size: var(--fs-xs); color: var(--text-muted); line-height: 1; }

.agenda { min-width: 0; }
.agenda-title { font-size: var(--fs-lg); }
.agenda-group + .agenda-group { margin-top: var(--sp-4); }
.agenda-date {
  font-size: var(--fs-sm);
  font-weight: var(--fw-bold);
  color: var(--text-muted);
  padding-bottom: var(--sp-1);
  margin-bottom: var(--sp-2);
  border-bottom: 1px solid var(--border);
}

/* =========================================================================
   13. Dialogs (§3.6–3.8)
   ========================================================================= */
.dlg {
  padding: 0;
  border: 0;
  background: var(--surface);
  box-shadow: var(--shadow-lg);
}
.dlg::backdrop { background: var(--backdrop); }

/* Form dialogs: full-screen on mobile */
.dlg-form {
  width: 100vw;
  max-width: 100vw;
  height: 100vh;               /* fallback for browsers without dvh */
  height: 100dvh;
  max-height: 100vh;
  max-height: 100dvh;
  margin: 0;
  border-radius: 0;
}
.dlg-form[open] { display: flex; flex-direction: column; }
.dlg-inner { display: flex; flex-direction: column; min-height: 0; flex: 1; }
.dlg-head {
  position: sticky;
  top: 0;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--sp-3);
  padding: var(--sp-2) var(--sp-2) var(--sp-2) var(--sp-4);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.dlg-title { font-size: var(--fs-xl); font-weight: var(--fw-bold); }
.dlg-body {
  flex: 1;
  overflow-y: auto;
  padding: var(--sp-4);
  display: flex;
  flex-direction: column;
  gap: var(--sp-4);
}
.dlg-foot {
  position: sticky;
  bottom: 0;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--sp-2);
  padding: var(--sp-3) var(--sp-4);
  background: var(--surface);
  border-top: 1px solid var(--border);
}
.dlg-foot-main { display: flex; gap: var(--sp-2); margin-left: auto; }

/* Confirm: always a centred box */
.dlg-confirm {
  width: min(420px, calc(100vw - 32px));
  border-radius: var(--r-lg);
}
.dlg-confirm .dlg-inner { padding: var(--sp-5); gap: var(--sp-3); }
.dlg-confirm .dlg-title { font-size: var(--fs-lg); }
.dlg-msg { overflow-wrap: anywhere; color: var(--text-muted); }
.dlg-confirm .dlg-foot { position: static; padding: var(--sp-2) 0 0; border-top: 0; }

/* =========================================================================
   14. Forms (§5.8)
   ========================================================================= */
.form-required-note { font-size: var(--fs-sm); color: var(--text-muted); }
.form-summary {
  padding: var(--sp-3);
  border-radius: var(--r-md);
  background: var(--danger-soft);
  color: var(--danger-soft-text);
  border: 1px solid var(--danger-text);
  font-size: var(--fs-sm);
  font-weight: var(--fw-semibold);
}
.summary-inner { display: flex; gap: var(--sp-2); align-items: flex-start; }
.summary-inner .icon { margin-top: 2px; }

.field { display: flex; flex-direction: column; gap: var(--sp-1); min-width: 0; }
.field-label { font-size: var(--fs-sm); font-weight: var(--fw-semibold); }
.input {
  width: 100%;
  min-height: var(--tap);
  padding: 0 var(--sp-3);
  font: inherit;
  font-size: var(--fs-md);
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--r-sm);
}
select.input { padding-right: var(--sp-2); }
.input[aria-invalid="true"] { border: 2px solid var(--danger-text); }
.field-hint { font-size: var(--fs-sm); color: var(--text-muted); }
.field-error, .field-warning {
  display: flex;
  align-items: flex-start;
  gap: var(--sp-1);
  font-size: var(--fs-sm);
  color: var(--danger-text);
}
.field-warning { color: var(--warning-text); background: var(--warning-soft); padding: var(--sp-1) var(--sp-2); border-radius: var(--r-sm); margin-top: var(--sp-2); }
.field-error .icon, .field-warning .icon { width: 16px; height: 16px; margin-top: 3px; }

.fieldset {
  margin: 0;
  padding: var(--sp-3);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: var(--sp-3);
}
.fieldset > legend { padding: 0 var(--sp-1); }
.field-row { display: grid; grid-template-columns: minmax(0, 1fr); gap: var(--sp-3); }

.check {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-2);
  min-height: var(--tap);
  cursor: pointer;
}
.check input { width: 20px; height: 20px; margin: 0; accent-color: var(--primary); }

.schedule-area, .exam-area { display: flex; flex-direction: column; gap: var(--sp-3); }
.schedule-rows { display: flex; flex-direction: column; gap: var(--sp-3); }
.schedule-row {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--sp-2) var(--sp-3);
  padding: var(--sp-3);
  background: var(--surface-2);
  border-radius: var(--r-md);
}
.schedule-row > .field:first-child { grid-column: 1 / -1; }
.schedule-row .field-room { grid-column: 1 / -1; }
.row-remove { grid-column: 1 / -1; display: flex; justify-content: flex-end; align-items: flex-end; }
.schedule-area > .btn { align-self: flex-start; }

/* colour picker */
.swatches { display: flex; flex-wrap: wrap; align-items: center; gap: var(--sp-2); }
.swatch { position: relative; display: inline-flex; padding: 2px; cursor: pointer; border-radius: var(--r-full); }
.swatch-dot {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: var(--r-full);
  background: var(--s-bg);
  border: 2px solid var(--s-accent);
  color: var(--s-text);
}
.swatch-dot .icon { visibility: hidden; }
.swatch input:checked + .swatch-dot { box-shadow: 0 0 0 2px var(--surface), 0 0 0 4px var(--text); }
.swatch input:checked + .swatch-dot .icon { visibility: visible; }
.swatch input:focus-visible + .swatch-dot { outline: 3px solid var(--focus); outline-offset: 4px; }
.color-current { font-size: var(--fs-sm); font-weight: var(--fw-semibold); margin-left: var(--sp-2); }
.preview-row { display: flex; align-items: center; gap: var(--sp-2); flex-wrap: wrap; }
.color-preview { width: 200px; }

/* =========================================================================
   15. Touch targets (§4)
   ========================================================================= */
@media (pointer: coarse) {
  .btn, .input, .nav-tab, .back-link { min-height: var(--tap); }
  .btn-icon { min-width: var(--tap); }
}

/* =========================================================================
   16. Tablet ≥ 768px
   ========================================================================= */
@media (min-width: 768px) {
  /* Fern I-6: Completed is a fixed column on wide screens, not collapsible */
  .board-done > .board-summary { cursor: default; }
  .board-done > .board-summary::before { display: none; }
  .container { padding: var(--sp-5); }
  .app-header-inner, .banner-area { padding-left: var(--sp-5); padding-right: var(--sp-5); }
  .view { gap: var(--sp-6); }
  .panel { padding: var(--sp-5); border-radius: var(--r-lg); box-shadow: var(--shadow-sm); }

  .btn { min-height: 40px; }
  .btn-icon { width: 40px; }
  .btn-sm { min-height: 36px; }
  .btn-sm.btn-icon { width: 36px; }

  /* chips wrap */
  .chip-scroller { margin: 0; }
  .chip-scroller::after { display: none; }
  .subject-chips { flex-wrap: wrap; overflow: visible; padding: 0; }
  .subject-chip { width: auto; min-width: 168px; max-width: 240px; }

  /* weekly time grid */
  .week {
    display: grid;
    grid-template-columns: var(--axis-w) repeat(5, minmax(0, 1fr));
    border: 1px solid var(--border);
    border-radius: var(--r-md);
    overflow: hidden;
  }
  .week-axis { display: block; border-right: 1px solid var(--border); }
  .week-head-spacer { height: var(--week-head-h); background: var(--surface-2); border-bottom: 1px solid var(--border); }
  .week-hours { list-style: none; position: relative; height: calc(var(--hours) * var(--hour-h)); }
  .week-hours li {
    position: absolute;
    top: calc(var(--i) * var(--hour-h));
    right: var(--sp-2);
    transform: translateY(-50%);
    font-size: var(--fs-xs);
    color: var(--text-muted);
    font-variant-numeric: tabular-nums;
  }
  .week-hours li:first-child { transform: none; }
  .week-hours li:last-child { transform: translateY(-100%); }
  .week-day, .week-day:first-of-type { padding: 0; border-top: 0; border-left: 1px solid var(--border); }
  .week-axis + .week-day { border-left: 0; }
  .week-day.is-today { border-left: 1px solid var(--border); padding-left: 0; }
  .week-day.is-today .week-day-head { background: var(--primary-soft); color: var(--primary); box-shadow: inset 0 -3px 0 var(--primary); }
  .week-day-head {
    justify-content: center;
    height: var(--week-head-h);
    margin: 0;
    background: var(--surface-2);
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
    overflow: hidden;
  }
  .day-full { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap; }
  .day-short { display: inline; }
  .today-badge { font-size: var(--fs-xs); }
  .week-col {
    position: relative;
    height: calc(var(--hours) * var(--hour-h));
    background: repeating-linear-gradient(to bottom, var(--border) 0 1px, transparent 1px var(--hour-h));
  }
  .week-slots { display: block; position: absolute; inset: 0; }
  .week-slot {
    position: absolute;
    top: calc(var(--start) / 60 * var(--hour-h) + 1px);
    height: calc(var(--slot-dur) / 60 * var(--hour-h) - 2px);
    left: calc(4px + (100% - 8px) * var(--lane) / var(--lanes));
    width: calc((100% - 8px) / var(--lanes) - 2px);
  }
  .class-block {
    display: flex;
    flex-direction: column;
    height: 100%;
    padding: var(--sp-1) var(--sp-2);
    line-height: 1.35;
  }
  .cb-code, .cb-time, .cb-room, .cb-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; overflow-wrap: normal; }
  .cb-name { display: none; }
  .week-empty { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap; }

  /* task board: 2 columns, Completed always shown */
  .board { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--sp-5); align-items: start; }
  .board-done { border-top: 0; padding-top: 0; }
  .task-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }

  /* detail */
  .subject-hero { padding: var(--sp-5); }
  .info-list { grid-template-columns: 140px minmax(0, 1fr); }
  .info-list dd { margin-bottom: var(--sp-2); }

  /* forms */
  .field-row-2 { grid-template-columns: 2fr 1fr; }
  .field-row { grid-template-columns: 2fr 1fr 1fr; }
  .exam-area .field-row { grid-template-columns: 2fr 1fr 1fr; }
  .field-row.field-row-2 { grid-template-columns: 2fr 1fr; }
  .schedule-row { grid-template-columns: 1.3fr 1fr 1fr 2fr auto; align-items: end; }
  .schedule-row > .field:first-child, .schedule-row .field-room, .row-remove { grid-column: auto; }

  .dlg-form {
    width: min(600px, calc(100vw - 48px));
    height: fit-content;       /* Fern I-3: short forms do not stretch to 90dvh */
    max-height: 90vh;
    max-height: 90dvh;
    margin: auto;
    border-radius: var(--r-lg);
  }
  .dlg-form .dlg-head { border-radius: var(--r-lg) var(--r-lg) 0 0; }
  .dlg-form .dlg-foot { border-radius: 0 0 var(--r-lg) var(--r-lg); }

  /* calendar */
  .cal-toolbar { grid-template-columns: auto 1fr auto auto; }
  .cal-toolbar > .btn-secondary { grid-column: auto; }
  .cal-cell { height: 80px; }
  .cal-day { align-items: stretch; padding: var(--sp-1); }
  .cal-daynum { align-self: flex-start; }
  .cal-pills { flex-direction: column; flex-wrap: nowrap; align-items: stretch; }
  .cal-pill {
    gap: var(--sp-1);
    padding: 1px var(--sp-1);
    border-radius: var(--r-sm);
    background: var(--s-bg);
    color: var(--s-text);
    border-left: 3px solid var(--s-accent);
    font-size: var(--fs-xs);
    line-height: 1.4;
    min-width: 0;
  }
  .cal-pill .shape { width: 7px; height: 7px; }
  .pill-short { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
  .cal-more { text-align: left; }
}

/* =========================================================================
   17. Desktop ≥ 1024px
   ========================================================================= */
@media (min-width: 1024px) {
  .day-full { position: static; width: auto; height: auto; overflow: visible; clip: auto; clip-path: none; }
  .day-short { display: none; }
  .cb-name { display: block; }

  .detail-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); align-items: start; }

  .cal-layout { grid-template-columns: minmax(0, 1fr) 320px; }
  .cal-cell { height: 96px; }
  .pill-short { display: none; }
  .pill-full { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
}
