:root {
  --primary: #1f5fa8;
  --primary-dark: #174a84;
  --bg: #f3f5f8;
  --card: #fff;
  --text: #222;
  --muted: #6b7280;
  --border: #d6dbe3;
  --danger: #c0392b;
  --ok: #1e8e4e;
  --warn: #b7791f;
}
* { box-sizing: border-box; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font-family: "Microsoft JhengHei", "PingFang TC", "Noto Sans TC", sans-serif; font-size: 15px; line-height: 1.6;
}
a { color: var(--primary); }
h1, h2, h3 { margin: 0 0 .6em; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.hidden { display: none !important; }
body:not(.is-admin) .admin-only { display: none !important; }

/* 頁首 */
#topbar {
  display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 8px;
  background: var(--primary); color: #fff; padding: 10px 20px;
}
#topbar a, #topbar .link { color: #fff; }
.brand { font-weight: bold; text-decoration: none; font-size: 17px; }
.brand span { font-weight: normal; margin-left: 10px; opacity: .85; font-size: 14px; }
.topbar-right { display: flex; gap: 16px; align-items: center; font-size: 14px; }

.container { max-width: 1100px; margin: 24px auto; padding: 0 16px; }
.card { background: var(--card); border: 1px solid var(--border); border-radius: 8px; padding: 20px 24px; margin-bottom: 18px; }

/* 按鈕與表單 */
button, .btn {
  display: inline-block; border: 1px solid var(--primary); background: var(--primary); color: #fff;
  padding: 7px 16px; border-radius: 5px; cursor: pointer; font-size: 14px; font-family: inherit; text-decoration: none;
}
button:hover, .btn:hover { background: var(--primary-dark); }
button.secondary, .btn.secondary { background: #fff; color: var(--primary); }
button.secondary:hover { background: #eef4fb; }
button.danger { background: #fff; color: var(--danger); border-color: var(--danger); }
button.danger:hover { background: #fdf0ee; }
button.small, .btn.small { padding: 3px 10px; font-size: 13px; }
button.link { background: none; border: none; padding: 0; color: var(--primary); text-decoration: underline; }
button:disabled { opacity: .5; cursor: not-allowed; }

input[type=text], input[type=password], input[type=email], input[type=tel], input[type=date], input[type=number], select, textarea {
  width: 100%; padding: 7px 10px; border: 1px solid var(--border); border-radius: 5px; font: inherit; background: #fff;
}
input:focus, select:focus, textarea:focus { outline: 2px solid #9cc2ee; border-color: var(--primary); }
input:disabled, select:disabled, textarea:disabled { background: #f4f5f7; color: #444; }
textarea { min-height: 80px; resize: vertical; }
label.field { display: block; margin-bottom: 12px; }
label.field > span { display: block; font-size: 14px; color: #444; margin-bottom: 3px; }
.req::after { content: " *"; color: var(--danger); }
.choice { display: inline-flex; align-items: center; gap: 4px; margin: 2px 14px 2px 0; cursor: pointer; }
.choice input { margin: 0; }
.choice-list .choice { display: flex; align-items: flex-start; margin: 4px 0; }
.choice-list .choice input { margin-top: 5px; }

.grid { display: grid; gap: 0 18px; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }
.grid-2 { display: grid; gap: 0 18px; grid-template-columns: 1fr 1fr; }
@media (max-width: 640px) { .grid-2 { grid-template-columns: 1fr; } }

fieldset { border: 1px solid var(--border); border-radius: 8px; padding: 14px 20px 6px; margin: 0 0 18px; background: #fff; }
legend { font-weight: bold; color: var(--primary); padding: 0 6px; font-size: 16px; }
.hint { font-size: 13px; color: var(--muted); margin: -6px 0 10px; }

/* 登入頁 */
.auth-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; }
.auth-card { width: 100%; max-width: 400px; }
.auth-card h1 { font-size: 20px; text-align: center; color: var(--primary); }
.auth-card .sub { text-align: center; color: var(--muted); margin: -8px 0 18px; }
.auth-card button[type=submit] { width: 100%; padding: 10px; font-size: 16px; }
.msg { padding: 8px 12px; border-radius: 5px; margin-bottom: 12px; font-size: 14px; }
.msg-error { background: #fdecea; color: var(--danger); }
.msg-ok { background: #e8f6ee; color: var(--ok); }
.msg-warn { background: #fff7e6; color: var(--warn); border: 1px solid #f3d9a4; }

/* 分頁 */
.tabs { display: flex; gap: 4px; border-bottom: 2px solid var(--border); margin-bottom: 18px; flex-wrap: wrap; }
.tabs button { background: none; color: var(--muted); border: none; border-bottom: 3px solid transparent; border-radius: 0; padding: 8px 16px; margin-bottom: -2px; font-size: 15px; }
.tabs button.active { color: var(--primary); border-bottom-color: var(--primary); font-weight: bold; }
.tabs button:hover { background: #eef2f7; }

/* 狀態 */
.badge { display: inline-block; padding: 1px 9px; border-radius: 10px; font-size: 12px; font-weight: bold; white-space: nowrap; }
.badge-draft { background: #eceff3; color: #555; }
.badge-submitted { background: #fff3d6; color: #9a6400; }
.badge-approved { background: #dcf3e5; color: var(--ok); }
.badge-returned { background: #fde2df; color: var(--danger); }
.status-bar { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 10px; }

/* 表格 */
table.list { width: 100%; border-collapse: collapse; background: #fff; font-size: 14px; }
table.list th, table.list td { border-bottom: 1px solid var(--border); padding: 8px 10px; text-align: left; vertical-align: middle; }
table.list th { background: #f0f3f7; font-weight: bold; white-space: nowrap; }
table.list tr:hover td { background: #f8fafc; }
.table-wrap { overflow-x: auto; }
.actions { display: flex; gap: 6px; flex-wrap: wrap; }
.toolbar { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin-bottom: 14px; }
.toolbar input, .toolbar select { width: auto; min-width: 160px; }
.toolbar input[type=checkbox] { min-width: 0; }
.toolbar .spacer { flex: 1; }

.exp-table { width: 100%; border-collapse: collapse; margin-bottom: 8px; }
.exp-table th, .exp-table td { border: 1px solid var(--border); padding: 4px; }
.exp-table th { background: #f0f3f7; font-size: 14px; }
.exp-table td.c { text-align: center; width: 48px; }

.qual-group { margin-bottom: 12px; }
.qual-group h4 { margin: 8px 0 4px; font-size: 14px; color: #333; }
.qual-group .docs { display: block; font-size: 12.5px; color: var(--muted); }

.form-actions { position: sticky; bottom: 0; background: rgba(243,245,248,.96); padding: 12px 0; display: flex; gap: 10px; justify-content: flex-end; border-top: 1px solid var(--border); z-index: 5; }

.file-cat { margin-bottom: 16px; }
.file-cat h4 { margin: 0 0 6px; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.upload-btn { font-weight: normal; }
.upload-btn.disabled { opacity: .6; pointer-events: none; }
.inline-files { background: #f8fafc; border: 1px dashed var(--border); border-radius: 6px; padding: 12px 14px; }
.file-item { display: flex; align-items: center; gap: 10px; padding: 6px 10px; border: 1px solid var(--border); border-radius: 5px; margin-bottom: 6px; background: #fafbfc; }
.file-item .name { flex: 1; word-break: break-all; }
.file-item img { width: 48px; height: 48px; object-fit: cover; border-radius: 3px; }

/* 對話框 */
.modal-bg { position: fixed; inset: 0; background: rgba(0,0,0,.4); display: flex; align-items: flex-start; justify-content: center; padding: 30px 12px; overflow-y: auto; z-index: 50; }
.modal { background: #fff; border-radius: 8px; width: 100%; max-width: 980px; padding: 20px 24px; position: relative; }
.modal.narrow { max-width: 440px; }
.modal .close { position: absolute; right: 12px; top: 10px; background: none; border: none; color: #666; font-size: 22px; padding: 0 6px; }

#toast { position: fixed; top: 16px; right: 16px; z-index: 100; display: flex; flex-direction: column; gap: 8px; max-width: 380px; }
.toast { padding: 10px 16px; border-radius: 6px; color: #fff; box-shadow: 0 4px 12px rgba(0,0,0,.15); }
.toast-ok { background: var(--ok); }
.toast-error { background: var(--danger); }

/* 紙本資料表版面 */
.sheet { background: #fff; color: #000; font-family: "DFKai-SB", "BiauKai", "標楷體", "Microsoft JhengHei", serif; }
.sheet-title { text-align: center; font-size: 22px; margin-bottom: 10px; }
.sheet-title small { font-size: 18px; }
.sheet-table { width: 100%; border-collapse: collapse; margin-bottom: 14px; font-size: 14px; table-layout: fixed; }
.sheet-table th, .sheet-table td { border: 1px solid #000; padding: 5px 6px; vertical-align: middle; word-break: break-word; }
.sheet-table th { font-weight: normal; text-align: center; background: #f7f7f7; }
.sheet-table th.group { text-align: left; font-weight: bold; background: #ececec; }
.sheet-table td.c { text-align: center; }
.sheet-table td.inner { padding: 0; }
.sheet-inner { width: 100%; border-collapse: collapse; }
.sheet-inner th, .sheet-inner td { border: none; border-right: 1px solid #000; border-bottom: 1px solid #000; padding: 4px 6px; }
.sheet-inner tr:last-child td { border-bottom: none; }
.sheet-inner th:last-child, .sheet-inner td:last-child { border-right: none; }
.sheet-table .cb { white-space: nowrap; margin-right: 4px; }
.sheet-table .paste { min-height: 120px; }
.sheet .sign { margin-top: 8px; text-align: right; }
.sheet-img { margin: 8px 0; text-align: center; page-break-inside: avoid; }
.sheet-img img { max-width: 100%; max-height: 900px; border: 1px solid #ccc; }
.sheet-img figcaption { font-size: 12px; color: #555; }
.qual-table td { font-size: 13px; }

/* 講師資料檢視（課程承辦人員） */
.unit-card { padding: 16px 20px; }
.unit-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; flex-wrap: wrap; margin-bottom: 8px; }
.unit-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 20px; }
@media (max-width: 640px) { .unit-grid { grid-template-columns: 1fr; } }
#unitSheets { display: none; }

@media print {
  body.printing-sheets .container, body.printing-sheets #topbar { display: none !important; }
  body.printing-sheets #unitSheets { display: block; }
  .print-sheet { page-break-after: always; }
  .print-sheet:last-child { page-break-after: auto; }
  body { background: #fff; }
  .no-print, #topbar { display: none !important; }
  .container { margin: 0; max-width: none; padding: 0; }
  .card { border: none; padding: 0; }
  .qual-table { page-break-before: always; }
  @page { size: A4; margin: 12mm; }
}

/* 課程場次 */
.session-table input, .session-table select { padding: 5px 6px; font-size: 14px; }
.session-table { min-width: 860px; }
.session-line { padding: 1px 0; border-bottom: 1px dotted #e3e7ee; }
.session-line:last-child { border-bottom: none; }

/* 依講師檢視 */
.seg { display: inline-flex; border: 1px solid var(--primary); border-radius: 5px; overflow: hidden; }
.seg button { border: none; border-radius: 0; background: #fff; color: var(--primary); padding: 6px 14px; }
.seg button.active { background: var(--primary); color: #fff; }
.lecturer-block { border: 1px solid var(--border); border-radius: 8px; padding: 14px 16px; margin-bottom: 14px; background: #fff; }

/* 課程行事曆 */
.cal-grid { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); border-top: 1px solid var(--border); border-left: 1px solid var(--border); }
.cal-head { background: #f0f3f7; text-align: center; font-weight: bold; padding: 6px 0; border-right: 1px solid var(--border); border-bottom: 1px solid var(--border); font-size: 14px; }
.cal-head.weekend { color: var(--danger); }
.cal-day { min-height: 110px; padding: 4px; border-right: 1px solid var(--border); border-bottom: 1px solid var(--border); background: #fff; min-width: 0; }
.cal-day.other { background: #f8f9fb; }
.cal-day.other .cal-date { color: #b5bcc7; }
.cal-day.today { background: #eef5ff; }
.cal-day.today .cal-date { background: var(--primary); color: #fff; border-radius: 10px; padding: 0 7px; display: inline-block; }
.cal-date { font-size: 13px; font-weight: bold; margin-bottom: 3px; }
.cal-event { display: block; width: 100%; text-align: left; border: none; border-left: 4px solid var(--border); background: #f4f6f9; color: var(--text);
  border-radius: 4px; padding: 3px 5px; margin-bottom: 4px; font-size: 12px; line-height: 1.35; cursor: pointer; overflow: hidden; }
.cal-event:hover { background: #e8eef7; }
.cal-event b { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cal-event .cal-time { color: var(--muted); }
.cal-event .cal-who { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cal-event.cal-confirmed { border-left-color: var(--ok); }
.cal-event.cal-pending { border-left-color: #2563eb; }
.cal-event.cal-returned { border-left-color: var(--danger); background: #fdf1f0; }
.cal-event.cal-missing { border-left-color: #e0a800; }
.cal-event.cal-overdue { border-left-color: var(--danger); background: #fdf1f0; }
.cal-event.cal-none { border-left-color: #9aa3af; }
.cal-tag { display: inline-block; background: #fde2df; color: var(--danger); border-radius: 3px; padding: 0 4px; font-size: 11px; }
.cal-legend { color: var(--muted); }
@media (max-width: 760px) {
  .cal-grid { display: none; }
}
@media print {
  body.printing-calendar * { visibility: hidden; }
  body.printing-calendar [data-view-panel="calendar"], body.printing-calendar [data-view-panel="calendar"] * { visibility: visible; }
  body.printing-calendar [data-view-panel="calendar"] { position: absolute; left: 0; top: 0; width: 100%; }
  body.printing-calendar .cal-toolbar button { display: none; }
  body.printing-calendar .cal-grid { display: grid; }
  body.printing-calendar .cal-day { min-height: 90px; }
}

/* 總行事曆 */
.owner-dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 4px; vertical-align: -1px; }
.master-event { background: #f7f8fa; }
.master-event .cal-who { font-size: 11.5px; }
@media print {
  body.printing-master * { visibility: hidden; }
  body.printing-master #masterMount, body.printing-master #masterMount * { visibility: visible; }
  body.printing-master #masterMount { position: absolute; left: 0; top: 0; width: 100%; }
  body.printing-master #masterMount .toolbar { display: none; }
  body.printing-master .cal-grid { display: grid; }
}

/* 講師端：每場次簡報 */
.session-card { border: 1px solid var(--border); border-radius: 6px; padding: 8px 12px; margin: 8px 0; background: #fafbfc; }

/* 問卷 */
.survey-q.missing { border-color: var(--danger); box-shadow: 0 0 0 2px #f8d0cb; }
.survey-opt { display: flex; padding: 6px 4px; margin: 0; }
.rating { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; }
.rating label { cursor: pointer; }
.rating input { position: absolute; opacity: 0; }
.rating span { display: flex; flex-direction: column; align-items: center; border: 1px solid var(--border); border-radius: 6px; padding: 8px 2px; background: #fff; }
.rating span b { font-size: 18px; }
.rating span small { font-size: 11px; color: var(--muted); text-align: center; }
.rating input:checked + span { background: var(--primary); border-color: var(--primary); color: #fff; }
.rating input:checked + span small { color: #fff; }
.rating input:focus-visible + span { outline: 2px solid #9cc2ee; }
.q-edit { border: 1px solid var(--border); border-radius: 6px; padding: 10px 12px; margin-bottom: 10px; background: #fafbfc; }
.q-edit .q-head { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 6px; }
.q-edit .q-head select { width: auto; }
.bar { height: 14px; background: #e6edf5; border-radius: 3px; overflow: hidden; min-width: 120px; }
.bar > i { display: block; height: 100%; background: var(--primary); }
.stat-row { display: grid; grid-template-columns: minmax(80px, 220px) 1fr 70px; gap: 10px; align-items: center; margin: 4px 0; font-size: 14px; }
.qr-box { display: flex; justify-content: center; margin: 10px 0; }

/* 講師資料表頁首說明 */
.msg-intro { background: #f3f8ff; border-color: #c9dcf5; }
