/* ============================================================
   pei-course — 共用樣式
   ============================================================ */

:root {
  --primary:      #1e40af;
  --primary-dark: #1e3a8a;
  --secondary:    #0284c7;
  --bg:           #f1f5f9;
  --card:         #ffffff;
  --text:         #1e293b;
  --text-soft:    #64748b;
  --border:       #cbd5e1;
  --border-soft:  #e2e8f0;
  --ok:           #059669;
  --warn:         #d97706;
  --danger:       #dc2626;
  --radius:       10px;
  --shadow:       0 1px 3px rgba(15, 23, 42, .08);
  --shadow-lg:    0 8px 24px rgba(15, 23, 42, .12);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body { height: 100%; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
               "PingFang TC", "Microsoft JhengHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

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

/* ---------- 頁首 ---------- */
.app-header {
  background: linear-gradient(135deg, var(--primary-dark), #0369a1);
  color: #fff;
  position: sticky;
  top: 0;
  z-index: 50;
  box-shadow: 0 2px 8px rgba(15, 23, 42, .2);
}
.hd-inner {
  max-width: 1200px;
  margin: 0 auto;
  padding: 10px 16px;
  display: flex;
  align-items: center;
  gap: 20px;
  flex-wrap: wrap;
}
.hd-brand {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #fff;
  text-decoration: none;
  font-weight: 700;
  font-size: 1.05rem;
  flex-shrink: 0;
}
.hd-logo {
  width: 30px; height: 30px;
  background: rgba(255,255,255,.2);
  border-radius: 8px;
  display: grid; place-items: center;
  font-size: .95rem;
}
.hd-nav { display: flex; gap: 4px; flex: 1; flex-wrap: wrap; }
.hd-nav a {
  color: #dbeafe;
  text-decoration: none;
  padding: 6px 12px;
  border-radius: 6px;
  font-size: .9rem;
  white-space: nowrap;
}
.hd-nav a:hover { background: rgba(255,255,255,.12); color: #fff; }
.hd-nav a.on    { background: rgba(255,255,255,.22); color: #fff; font-weight: 600; }
.hd-user { display: flex; align-items: center; gap: 10px; font-size: .85rem; }
.hd-name { color: #e0f2fe; max-width: 180px; overflow: hidden;
           text-overflow: ellipsis; white-space: nowrap; }
.hd-role {
  padding: 2px 8px;
  border-radius: 999px;
  font-size: .72rem;
  font-weight: 700;
}
.hd-role-teacher { background: #fbbf24; color: #78350f; }
.hd-role-student { background: #a7f3d0; color: #065f46; }

/* ---------- 版面 ---------- */
.page {
  max-width: 1200px;
  margin: 0 auto;
  padding: 24px 16px 60px;
}
.page-narrow { max-width: 780px; }

.page-head {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 20px;
}
.page-head h1 { font-size: 1.5rem; color: var(--primary-dark); }
.page-head p  { color: var(--text-soft); font-size: .9rem; margin-top: 2px; }

/* ---------- 卡片 ---------- */
.card {
  background: var(--card);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  margin-bottom: 18px;
}
.card-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; flex-wrap: wrap;
  margin-bottom: 14px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--border-soft);
}
.card-head h2 { font-size: 1.1rem; color: var(--primary-dark); }
.card-head p  { font-size: .85rem; color: var(--text-soft); }

/* ---------- 統計磚 ---------- */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 14px;
  margin-bottom: 20px;
}
.stat {
  background: var(--card);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  padding: 16px 18px;
  box-shadow: var(--shadow);
}
.stat-label { font-size: .8rem; color: var(--text-soft); margin-bottom: 4px; }
.stat-value { font-size: 1.8rem; font-weight: 700; color: var(--primary-dark);
              line-height: 1.2; }
.stat-note  { font-size: .78rem; color: var(--text-soft); margin-top: 2px; }

/* ---------- 按鈕 ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  background: var(--primary);
  color: #fff;
  border: 1px solid transparent;
  padding: 8px 16px;
  border-radius: 8px;
  font-size: .9rem;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  text-decoration: none;
  transition: filter .15s, background .15s;
  white-space: nowrap;
}
.btn:hover:not(:disabled) { filter: brightness(1.08); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-sm    { padding: 5px 11px; font-size: .82rem; }
.btn-block { width: 100%; }
.btn-sec   { background: #fff; color: var(--text); border-color: var(--border); }
.btn-sec:hover:not(:disabled)   { background: #f8fafc; }
.btn-ghost { background: rgba(255,255,255,.15); color: #fff;
             border-color: rgba(255,255,255,.35); }
.btn-danger{ background: var(--danger); }
.btn-ok    { background: var(--ok); }
.btn-warn  { background: var(--warn); }

.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }

/* ---------- 表單 ---------- */
.field { margin-bottom: 14px; }
.field label {
  display: block;
  font-size: .85rem;
  font-weight: 600;
  color: #334155;
  margin-bottom: 5px;
}
.field .hint { font-size: .78rem; color: var(--text-soft); font-weight: 400; }

input[type=text], input[type=email], input[type=password],
input[type=number], input[type=date], input[type=datetime-local],
select, textarea {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: .92rem;
  font-family: inherit;
  color: var(--text);
  background: #fff;
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--secondary);
  box-shadow: 0 0 0 3px rgba(2, 132, 199, .15);
}
textarea { resize: vertical; min-height: 90px; line-height: 1.6; }

.field-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 14px;
}

.check-row { display: flex; align-items: center; gap: 8px; }
.check-row input[type=checkbox] { width: 16px; height: 16px; cursor: pointer; }
.check-row label { margin: 0; cursor: pointer; font-weight: 500; }

.check-list {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  max-height: 220px;
  overflow-y: auto;
  background: #fff;
}
.check-list .check-row { padding: 4px 0; }

/* ---------- 表格 ---------- */
.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  background: #fff;
}
table.data {
  width: 100%;
  border-collapse: collapse;
  font-size: .88rem;
  min-width: 620px;
}
table.data th, table.data td {
  padding: 10px 12px;
  text-align: left;
  border-bottom: 1px solid var(--border-soft);
  vertical-align: middle;
}
table.data th {
  background: #f8fafc;
  font-weight: 600;
  color: #334155;
  font-size: .82rem;
  white-space: nowrap;
  position: sticky;
  top: 0;
}
table.data tbody tr:hover { background: #f8fafc; }
table.data tbody tr:last-child td { border-bottom: none; }
td.actions { white-space: nowrap; }
td.actions .btn { margin-right: 4px; }

/* ---------- 標籤 ---------- */
.tag {
  display: inline-block;
  padding: 2px 9px;
  border-radius: 999px;
  font-size: .75rem;
  font-weight: 600;
  white-space: nowrap;
}
.tag-teacher  { background: #fef3c7; color: #92400e; }
.tag-student  { background: #d1fae5; color: #065f46; }
.tag-on       { background: #dcfce7; color: #166534; }
.tag-off      { background: #fee2e2; color: #991b1b; }
.tag-draft    { background: #e2e8f0; color: #475569; }
.tag-pub      { background: #dbeafe; color: #1e40af; }
.tag-graded   { background: #ede9fe; color: #5b21b6; }
.tag-late     { background: #ffedd5; color: #9a3412; }
.tag-junior   { background: #dbeafe; color: #1e40af; }
.tag-senior   { background: #fce7f3; color: #9d174d; }

/* ---------- 對話框 ---------- */
.modal-mask {
  position: fixed; inset: 0;
  background: rgba(15, 23, 42, .55);
  display: none;
  align-items: flex-start;
  justify-content: center;
  padding: 40px 16px;
  z-index: 200;
  overflow-y: auto;
}
.modal-mask.open { display: flex; }
.modal {
  background: #fff;
  border-radius: 12px;
  box-shadow: var(--shadow-lg);
  width: 100%;
  max-width: 540px;
  overflow: hidden;
  animation: pop .16s ease-out;
}
.modal-wide { max-width: 780px; }
@keyframes pop {
  from { transform: translateY(-12px); opacity: 0; }
  to   { transform: none; opacity: 1; }
}
.modal-head {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border-soft);
  display: flex; align-items: center; justify-content: space-between;
}
.modal-head h3 { font-size: 1.05rem; color: var(--primary-dark); }
.modal-x {
  background: none; border: none; font-size: 1.5rem;
  color: var(--text-soft); cursor: pointer; line-height: 1;
  padding: 0 4px;
}
.modal-body { padding: 20px; }
.modal-foot {
  padding: 14px 20px;
  border-top: 1px solid var(--border-soft);
  display: flex; justify-content: flex-end; gap: 8px;
  background: #f8fafc;
  flex-wrap: wrap;
}

/* ---------- 提示訊息 ---------- */
.alert {
  padding: 11px 14px;
  border-radius: 8px;
  font-size: .87rem;
  margin-bottom: 14px;
  border-left: 4px solid;
}
.alert-info   { background: #eff6ff; border-color: var(--secondary); color: #075985; }
.alert-ok     { background: #f0fdf4; border-color: var(--ok);        color: #14532d; }
.alert-warn   { background: #fffbeb; border-color: var(--warn);      color: #78350f; }
.alert-danger { background: #fef2f2; border-color: var(--danger);    color: #7f1d1d; }
.alert.hidden { display: none; }

/* ---------- Toast ---------- */
.toast-box {
  position: fixed;
  bottom: 22px; left: 50%;
  transform: translateX(-50%);
  display: flex; flex-direction: column; gap: 8px;
  z-index: 500;
  pointer-events: none;
}
.toast {
  background: #0f172a;
  color: #fff;
  padding: 10px 20px;
  border-radius: 999px;
  font-size: .88rem;
  box-shadow: var(--shadow-lg);
  opacity: 0;
  transform: translateY(12px);
  transition: opacity .25s, transform .25s;
}
.toast-show   { opacity: 1; transform: none; }
.toast-ok     { background: var(--ok); }
.toast-error  { background: var(--danger); }
.toast-warn   { background: var(--warn); }

/* ---------- 載入 / 空狀態 ---------- */
.loading {
  padding: 50px 20px;
  text-align: center;
  color: var(--text-soft);
  font-size: .9rem;
}
.spinner {
  width: 30px; height: 30px;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin .7s linear infinite;
  margin: 0 auto 12px;
}
@keyframes spin { to { transform: rotate(360deg); } }

.empty {
  padding: 48px 20px;
  text-align: center;
  color: var(--text-soft);
}
.empty-icon { font-size: 2.4rem; margin-bottom: 10px; opacity: .5; }
.empty p    { font-size: .92rem; margin-bottom: 4px; }
.empty .sub { font-size: .82rem; }

/* ---------- 課程卡 ---------- */
.course-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 16px;
}
.course-card {
  background: #fff;
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  text-decoration: none;
  color: inherit;
  transition: transform .15s, box-shadow .15s;
}
a.course-card:hover { transform: translateY(-2px); box-shadow: var(--shadow-lg); }
.cc-top {
  height: 6px;
  background: linear-gradient(90deg, var(--primary), var(--secondary));
}
.cc-body { padding: 14px 16px; flex: 1; }
.cc-tags { display: flex; gap: 6px; margin-bottom: 8px; flex-wrap: wrap; }
.cc-title { font-size: 1.02rem; font-weight: 700; color: var(--primary-dark);
            margin-bottom: 4px; }
.cc-desc { font-size: .84rem; color: var(--text-soft);
           display: -webkit-box; -webkit-line-clamp: 2;
           -webkit-box-orient: vertical; overflow: hidden; }
.cc-foot {
  padding: 9px 16px;
  border-top: 1px solid var(--border-soft);
  font-size: .8rem;
  color: var(--text-soft);
  display: flex; justify-content: space-between; align-items: center;
  gap: 8px; flex-wrap: wrap;
}

/* ---------- 單元清單 ---------- */
.lesson-list { display: flex; flex-direction: column; gap: 8px; }
.lesson-item {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 14px;
  background: #fff;
  border: 1px solid var(--border-soft);
  border-radius: 8px;
  text-decoration: none;
  color: inherit;
}
a.lesson-item:hover { border-color: var(--secondary); background: #f8fafc; }
.li-num {
  width: 28px; height: 28px;
  background: #eff6ff; color: var(--primary);
  border-radius: 6px;
  display: grid; place-items: center;
  font-size: .82rem; font-weight: 700;
  flex-shrink: 0;
}
.li-main { flex: 1; min-width: 0; }
.li-title { font-weight: 600; font-size: .95rem; }
.li-sub   { font-size: .8rem; color: var(--text-soft); }

/* ---------- 登入頁 ---------- */
.auth-wrap {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px 16px;
  background: linear-gradient(135deg, #1e3a8a, #0369a1);
}
.auth-card {
  background: #fff;
  border-radius: 14px;
  box-shadow: 0 20px 50px rgba(0,0,0,.28);
  width: 100%;
  max-width: 400px;
  padding: 32px 28px;
}
.auth-logo {
  width: 54px; height: 54px;
  background: linear-gradient(135deg, var(--primary), var(--secondary));
  border-radius: 14px;
  display: grid; place-items: center;
  color: #fff; font-size: 1.5rem; font-weight: 700;
  margin: 0 auto 14px;
}
.auth-card h1 { font-size: 1.3rem; text-align: center; color: var(--primary-dark); }
.auth-card .sub {
  text-align: center; color: var(--text-soft);
  font-size: .87rem; margin: 4px 0 22px;
}
.auth-sep {
  display: flex; align-items: center; gap: 10px;
  color: var(--text-soft); font-size: .78rem;
  margin: 18px 0;
}
.auth-sep::before, .auth-sep::after {
  content: ""; flex: 1; height: 1px; background: var(--border);
}
.auth-foot {
  text-align: center; margin-top: 18px;
  font-size: .82rem; color: var(--text-soft);
}
.auth-foot a { cursor: pointer; }

/* ---------- 講義閱讀器 ---------- */
.reader-bar {
  background: #0f172a;
  color: #fff;
  padding: 8px 16px;
  display: flex; align-items: center; gap: 12px;
  position: sticky; top: 0; z-index: 60;
  flex-wrap: wrap;
}
.reader-bar a { color: #cbd5e1; text-decoration: none; font-size: .85rem; }
.reader-bar .rb-title { flex: 1; font-size: .92rem; font-weight: 600;
                        overflow: hidden; text-overflow: ellipsis;
                        white-space: nowrap; }
.reader-frame {
  width: 100%;
  height: calc(100vh - 42px);
  border: none;
  display: block;
  background: #fff;
}

/* ---------- 其他 ---------- */
.muted    { color: var(--text-soft); }
.small    { font-size: .82rem; }
.mono     { font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
            font-size: .84rem; }
.center   { text-align: center; }
.mt-2     { margin-top: 16px; }
.mb-2     { margin-bottom: 16px; }
.nowrap   { white-space: nowrap; }
.hidden   { display: none !important; }
.grow     { flex: 1; }

.filter-bar {
  display: flex; gap: 10px; flex-wrap: wrap;
  align-items: center; margin-bottom: 16px;
}
.filter-bar input, .filter-bar select { width: auto; min-width: 150px; }
.filter-bar .grow { min-width: 200px; }

/* ============================================================
   手機版
   ============================================================ */

@media (max-width: 720px) {

  /* ---------- 頁首：兩行排列，導覽列橫向捲動 ---------- */
  .hd-inner {
    gap: 8px 10px;
    padding: 8px 12px;
  }
  .hd-brand  { order: 1; }
  .hd-user   { order: 2; margin-left: auto; }
  .hd-nav    {
    order: 3;
    flex: 1 0 100%;
    overflow-x: auto;
    flex-wrap: nowrap;
    padding-bottom: 2px;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  .hd-nav::-webkit-scrollbar { display: none; }
  .hd-nav a  { flex-shrink: 0; padding: 7px 12px; }
  .hd-name   { display: none; }
  .hd-title  { font-size: .98rem; }

  .page         { padding: 14px 12px 60px; }
  .page-head    { gap: 10px; margin-bottom: 14px; }
  .page-head h1 { font-size: 1.22rem; }
  .card         { padding: 15px 13px; margin-bottom: 14px; }

  /* ---------- 手指點得到的按鈕 ---------- */
  .btn {
    padding: 10px 16px;
    min-height: 42px;
  }
  .btn-sm { padding: 8px 12px; min-height: 38px; font-size: .85rem; }
  .btn-row .btn { flex: 1 1 auto; }
  .page-head .btn-row { width: 100%; }

  /* ---------- 表單：iOS 會對 16px 以下的欄位自動放大，所以固定 16px ---------- */
  input[type=text], input[type=email], input[type=password],
  input[type=number], input[type=date], input[type=datetime-local],
  select, textarea {
    font-size: 16px;
    padding: 11px 13px;
  }
  .field-row { grid-template-columns: 1fr; gap: 0; }
  .field-row .field { margin-bottom: 14px; }

  .filter-bar { gap: 8px; }
  .filter-bar input,
  .filter-bar select,
  .filter-bar .grow { width: 100%; min-width: 0; flex: 1 1 100%; }
  .filter-bar > span { width: 100%; }

  /* ---------- 表格改成卡片 ---------- */
  /*
     每個 td 靠 data-label 顯示欄位名稱。
     JS 那邊會自動從 thead 抓標題填進去，
     所以新增表格不用手動標記。
  */
  .table-wrap {
    border: none;
    background: transparent;
    overflow: visible;
  }
  table.data {
    min-width: 0;
    border-collapse: separate;
    border-spacing: 0 10px;
  }
  table.data thead { display: none; }
  table.data tbody, table.data tr, table.data td { display: block; width: 100%; }

  table.data tr {
    background: #fff;
    border: 1px solid var(--border-soft);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 12px 14px;
  }
  table.data tbody tr:hover { background: #fff; }

  table.data td {
    border: none;
    padding: 5px 0;
    display: flex;
    gap: 12px;
    align-items: flex-start;
    justify-content: space-between;
    text-align: right;
  }
  table.data td::before {
    content: attr(data-label);
    font-size: .78rem;
    font-weight: 600;
    color: var(--text-soft);
    text-align: left;
    flex-shrink: 0;
    padding-top: 1px;
  }
  /* 沒有標題的欄位（例如純操作欄）不留空位 */
  table.data td[data-label=""]::before { display: none; }

  /* 第一欄當成卡片標題，獨佔一行 */
  table.data td:first-child {
    display: block;
    text-align: left;
    padding: 0 0 8px;
    margin-bottom: 6px;
    border-bottom: 1px solid var(--border-soft);
    font-size: 1rem;
  }
  table.data td:first-child::before { display: none; }

  /* 操作按鈕橫排撐滿 */
  table.data td.actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    justify-content: flex-start;
    padding-top: 12px;
    margin-top: 6px;
    border-top: 1px solid var(--border-soft);
  }
  table.data td.actions::before { display: none; }
  table.data td.actions .btn { margin: 0; flex: 1 1 auto; }
  table.data td.center { text-align: right; }

  /* ---------- 對話框：從底部滑上來，接近原生 App ---------- */
  .modal-mask {
    padding: 0;
    align-items: flex-end;
  }
  .modal, .modal-wide {
    max-width: 100%;
    max-height: 92vh;
    border-radius: 16px 16px 0 0;
    display: flex;
    flex-direction: column;
    animation: slideUp .22s ease-out;
  }
  @keyframes slideUp {
    from { transform: translateY(100%); }
    to   { transform: none; }
  }
  .modal-head { padding: 14px 16px; flex-shrink: 0; }
  .modal-body {
    padding: 16px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    flex: 1;
  }
  .modal-foot {
    padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
    flex-shrink: 0;
  }
  .modal-foot .btn { flex: 1; }
  .modal-x { font-size: 1.8rem; padding: 0 8px; }

  /* ---------- 其他 ---------- */
  .stat-grid   { grid-template-columns: repeat(2, 1fr); gap: 10px; }
  .stat        { padding: 13px 14px; }
  .stat-value  { font-size: 1.5rem; }
  .course-grid { grid-template-columns: 1fr; }
  .lesson-item { padding: 13px 12px; }
  .toast-box   { bottom: 16px; left: 12px; right: 12px; transform: none; }
  .toast       { text-align: center; }
  .reader-frame { height: calc(100vh - 44px); height: calc(100dvh - 44px); }
}

@media (max-width: 380px) {
  .stat-grid { grid-template-columns: 1fr; }
  .hd-title  { display: none; }
}
