/* ===========================
   KAY Studio / 課表系統 styles.css
   - Mobile-first
   - Dark mode (:root.dark)
   - A11y focus / reduced motion
   - Responsive forms, tables, grids
   =========================== */

/* ---------- 顏色主題 ---------- */
:root{
  --bg: #ffffff;
  --fg: #111111;
  --muted: #6b7280;
  --card: #f9fafb;
  --border: #e5e7eb;

  --primary: #2563eb;
  --primary-press: #1d4ed8;
  --success: #16a34a;
  --danger: #e11d48;
  --warning: #d97706;
  --info: #0891b2;

  --radius-lg: 16px;
  --radius-md: 10px;
  --radius-sm: 8px;

  --shadow-1: 0 1px 2px rgba(0,0,0,.05);
  --shadow-2: 0 6px 16px rgba(0,0,0,.08);

  --container: 960px;
}

:root.dark{
  --bg: #0b0f17;
  --fg: #e5e7eb;
  --muted: #9ca3af;
  --card: #111827;
  --border: #1f2937;

  --primary: #60a5fa;
  --primary-press: #3b82f6;

  --shadow-1: 0 1px 2px rgba(0,0,0,.4);
  --shadow-2: 0 10px 24px rgba(0,0,0,.4);
}

/* ---------- Reset / Base ---------- */
* { box-sizing: border-box; }
html, body { height: 100%; }
html { -webkit-text-size-adjust: 100%; }
body{
  margin: 0;
  font-family: system-ui, -apple-system, Segoe UI, Roboto, Arial, "Noto Sans TC", "PingFang TC", "Heiti TC", sans-serif;
  background: var(--bg);
  color: var(--fg);
  line-height: 1.55;
  -webkit-tap-highlight-color: transparent;
}
img, svg, video { max-width: 100%; height: auto; }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

/* 低動態需求 */
@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .001ms !important; animation-iteration-count: 1 !important; transition-duration: .001ms !important; scroll-behavior: auto !important; }
}

/* ---------- 容器 ---------- */
.container { max-width: var(--container); margin: 0 auto; padding: 16px; }
.container-narrow { max-width: 720px; }

/* ---------- Header（頂端固定、行動友善） ---------- */
.header{
  position: sticky;
  top: 0;
  z-index: 20;
  background: color-mix(in oklab, var(--bg) 92%, transparent);
  backdrop-filter: saturate(160%) blur(8px);
  border-bottom: 1px solid var(--border);
}
.header-inner{
  max-width: var(--container);
  margin: 0 auto;
  padding: 12px 16px;
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
}
.header .title{
  font-weight: 700;
  font-size: clamp(16px, 2.6vw, 20px);
  margin-right: auto;
}

/* ---------- 元件：輸入 / 選單 / 按鈕 ---------- */
.input, .select, .textarea, .btn{
  height: 40px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--fg);
  border-radius: var(--radius-md);
  padding: 0 12px;
  font-size: 14px;
}

.textarea{
  min-height: 96px;
  padding: 10px 12px;
  resize: vertical;
}

.input::placeholder, .textarea::placeholder { color: color-mix(in oklab, var(--muted) 80%, transparent); }
select.select { padding-right: 28px; }

.btn{
  border: none;
  background: var(--primary);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
  box-shadow: var(--shadow-1);
  transition: transform .08s ease;
}
.btn:active{ transform: translateY(1px); background: var(--primary-press); }
.btn[disabled], .btn:disabled{ opacity:.6; cursor:not-allowed; }

.btn.ghost{
  background: transparent;
  color: var(--fg);
  border: 1px solid var(--border);
}
.btn.secondary{
  background: color-mix(in oklab, var(--primary) 10%, var(--bg));
  color: var(--fg);
  border: 1px solid var(--border);
}
.btn.success{ background: var(--success); }
.btn.danger{ background: var(--danger); }
.btn.warning{ background: var(--warning); }
.btn.info{ background: var(--info); }

/* 小尺寸按鈕 */
.btn.sm{ height: 32px; padding: 0 10px; font-size: 13px; border-radius: 8px; }

/* ---------- 標籤 / 徽章 / 提示 ---------- */
.tag{
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  border: 1px solid var(--border);
  font-size: 12px;
  color: var(--fg);
}
.badge{
  font-size: 12px;
  color: var(--muted);
  padding: 4px 8px;
  border: 1px dashed var(--border);
  border-radius: 999px;
}
.notice{ color: var(--muted); font-size: 13px; }

.alert{
  border-radius: var(--radius-md);
  padding: 10px 12px;
  border: 1px solid var(--border);
  background: var(--card);
}
.alert.success{ border-color: color-mix(in oklab, var(--success) 40%, var(--border)); }
.alert.danger{ border-color: color-mix(in oklab, var(--danger) 40%, var(--border)); }

/* ---------- 卡片 / 網格 ---------- */
.card{
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 14px;
  box-shadow: var(--shadow-1);
}

.grid{ display: grid; gap: 12px; grid-template-columns: 1fr; }
.grid-cards{ display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); }
.stats{ display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); }
.stat .n{ font-size: 28px; font-weight: 800; }

/* ---------- 表單版面（行動優先） ---------- */
.row{ display: grid; gap: 12px; grid-template-columns: 1fr; }
.row-2{ grid-template-columns: 1fr; }
.row-3{ grid-template-columns: 1fr; }

label{ font-size: 13px; color: var(--muted); }

/* >=640px 以上做欄位分欄 */
@media (min-width: 640px){
  .row-2{ grid-template-columns: 1fr 1fr; }
  .row-3{ grid-template-columns: 1fr 1fr 1fr; }
}

/* ---------- 工具列 ---------- */
.toolbar{ display:flex; gap:8px; align-items:center; flex-wrap:wrap; margin:12px 0; }
.toolbar .spacer{ flex: 1; }

/* ---------- 表格（可橫向捲動 / 可選擇堆疊） ---------- */
.table-wrap{ width: 100%; overflow-x:auto; border:1px solid var(--border); border-radius: var(--radius-lg); background: var(--card); }
.table{ width: 100%; border-collapse: collapse; min-width: 640px; }
.table th, .table td{ border-bottom: 1px solid var(--border); padding: 10px; text-align: left; }
.table thead th{ font-size: 13px; color: var(--muted); position: sticky; top: 0; background: var(--card); }

/* 堆疊版（需在 <td> 放 data-label 才能顯示標頭文字） */
.table.stacked{ min-width: 0; border-collapse: separate; border-spacing: 0; }
.table.stacked thead{ display: none; }
.table.stacked tr{ display: block; border-bottom: 1px solid var(--border); }
.table.stacked td{
  display: grid;
  grid-template-columns: 120px 1fr;
  gap: 8px;
  border: none;
  padding: 10px 0;
}
.table.stacked td::before{
  content: attr(data-label);
  color: var(--muted);
  font-size: 12px;
}

/* ---------- 頁尾 ---------- */
footer{
  border-top: 1px solid var(--border);
  text-align: center;
  color: var(--muted);
  padding: 16px;
}

/* ---------- 返回頂端按鈕（若有） ---------- */
#backToTop{
  position: fixed;
  right: 16px;
  bottom: 16px;
  z-index: 30;
  display: none;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--fg);
  cursor: pointer;
  box-shadow: var(--shadow-2);
}

/* ---------- 小工具 ---------- */
.hidden { display: none !important; }
.sr-only{
  position: absolute !important;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0;
}

/* ---------- 印刷友善 ---------- */
@media print{
  .header, #backToTop, .toolbar { display:none !important; }
  .container { padding-top: 0; }
  .card { break-inside: avoid; }
  a { text-decoration: none; color: #000; }
}

/* ---------- 版面微調（寬螢幕） ---------- */
@media (min-width: 1024px){
  .container{ padding: 20px; }
  .header-inner{ padding: 14px 16px; }
}

/* ---------- 陰影/互動效果 ---------- */
.card:hover{ transform: translateY(-1px); transition: transform .08s ease; }
.btn:hover{ filter: brightness(1.02); }

/* ---------- 外觀細節 ---------- */
hr{ border: none; height: 1px; background: var(--border); margin: 16px 0; }
code,kbd,pre,samp{ font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; }
pre{ background: var(--card); border: 1px solid var(--border); padding: 10px; border-radius: var(--radius-md); overflow:auto; }

/* ---------- 安全區域（iOS 底部） ---------- */
.safe-bottom{ padding-bottom: max(16px, env(safe-area-inset-bottom)); }
.safe-top{ padding-top: max(0px, env(safe-area-inset-top)); }
