:root {
    --ink: #16303c;
    --muted: #5b7380;
    --paper: #f3efe4;
    --board: #1f6f5b;
    --seat: #fffdf8;
    --line: #d9d0c1;
    --accent: #d9852b;
    --primary: #1b3a4b;
    --plus: #2c7a4b;
    --minus: #b4413c;
    --shadow: 0 10px 24px rgba(22, 48, 60, 0.12);
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
  }

  * { box-sizing: border-box; }

  html, body {
    margin: 0;
    min-height: 100%;
    background: var(--paper);
    color: var(--ink);
    font-family: "Noto Sans TC", "Microsoft JhengHei", sans-serif;
    overscroll-behavior: none;
  }

  #app {
    min-height: 100dvh;
    display: grid;
    grid-template-rows: auto auto 1fr;
    padding: calc(12px + var(--safe-top)) 12px calc(12px + var(--safe-bottom));
    gap: 10px;
  }

  .topbar, .toolbar, .board-wrap, .side, .modal-card {
    background: #fffdf8;
    border: 1px solid var(--line);
    border-radius: 18px;
    box-shadow: var(--shadow);
  }

  .topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 12px 16px;
    flex-wrap: wrap;
  }

  .brand {
    display: flex;
    align-items: center;
    gap: 12px;
  }

  .brand-mark {
    width: 42px;
    height: 42px;
    display: grid;
    place-items: center;
    border-radius: 12px;
    background: var(--primary);
    color: #fff;
    font-weight: 700;
  }

  h1, h2 { margin: 0; font-size: 18px; }
  .topbar p, .hint, .lottery-meta { margin: 4px 0 0; color: var(--muted); font-size: 13px; }

  .topbar-actions {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: flex-end;
    gap: 10px;
  }

  .class-picker {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 14px;
  }

  .mode-switch {
    display: flex;
    gap: 4px;
    padding: 4px;
    background: #eef4f7;
    border-radius: 14px;
  }

  .mode-switch .tool {
    min-width: 96px;
  }

  .mode-switch .tab-on {
    background: var(--primary);
    color: #fff;
    border-color: var(--primary);
  }

  select, input, textarea, button {
    font: inherit;
  }

  select, input, textarea {
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 10px 12px;
    background: #fff;
    color: var(--ink);
  }

  .toolbar {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    padding: 10px;
    align-items: center;
  }

  .tool, .chip {
    min-height: 44px;
    min-width: 44px;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: #fff;
    color: var(--ink);
    padding: 0 14px;
    cursor: pointer;
  }

  .tool.primary {
    background: var(--primary);
    color: #fff;
    border-color: var(--primary);
  }

  .tool.primary.need-sync {
    box-shadow: 0 0 0 3px rgba(196, 122, 42, 0.45);
    animation: need-sync-pulse 1.6s ease-in-out infinite;
  }

  @keyframes need-sync-pulse {
    50% { box-shadow: 0 0 0 6px rgba(196, 122, 42, 0.15); }
  }

  .tool.accent {
    background: var(--accent);
    color: #fff;
    border-color: var(--accent);
  }

  .tool.active-plus {
    background: var(--plus);
    color: #fff;
    border-color: var(--plus);
  }

  .tool.active-minus {
    background: var(--minus);
    color: #fff;
    border-color: var(--minus);
  }

  .tool.active-group {
    background: #3d6b8a;
    color: #fff;
    border-color: #3d6b8a;
  }

  .chip.active {
    background: var(--ink);
    color: #fff;
    border-color: var(--ink);
  }

  .delta-group { display: flex; gap: 6px; }

  .stage {
    min-height: 0;
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(300px, 360px);
    gap: 10px;
  }

  .board-wrap {
    min-height: 0;
    display: flex;
    flex-direction: column;
    padding: 12px;
    gap: 10px;
  }

  .board-toolbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
  }

  .layout-switch {
    display: flex;
    gap: 6px;
  }

  .layout-switch .tool.tab-on {
    background: var(--primary);
    color: #fff;
    border-color: var(--primary);
  }

  .board-toolbar .hint {
    margin: 0;
  }

  .timer-panel {
    display: grid;
    grid-template-columns: minmax(160px, 220px) 1fr;
    gap: 12px 18px;
    align-items: center;
    margin: 0 14px 10px;
    padding: 12px 16px;
    border: 1px solid var(--line);
    border-radius: 16px;
    background:
      linear-gradient(135deg, rgba(27, 58, 75, 0.08), rgba(255, 253, 248, 0.95) 55%),
      #fffdf8;
    box-shadow: var(--shadow);
  }

  .timer-panel[hidden] {
    display: none !important;
  }

  .timer-panel.running {
    border-color: #2c7a4b;
    box-shadow: 0 0 0 2px rgba(44, 122, 75, 0.18);
  }

  .timer-panel.warn {
    border-color: #c27a1a;
    background:
      linear-gradient(135deg, rgba(243, 200, 75, 0.28), rgba(255, 253, 248, 0.95) 55%),
      #fffdf8;
  }

  .timer-panel.urgent {
    border-color: var(--minus);
    background:
      linear-gradient(135deg, rgba(196, 74, 66, 0.18), rgba(255, 253, 248, 0.95) 55%),
      #fffdf8;
    animation: timer-pulse 0.9s ease-in-out infinite;
  }

  .timer-panel.done {
    border-color: var(--minus);
    background: #f8d4d2;
    animation: none;
  }

  @keyframes timer-pulse {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.01); }
  }

  .timer-face {
    text-align: center;
  }

  .timer-label {
    margin: 0 0 2px;
    font-size: 13px;
    font-weight: 700;
    color: var(--muted);
    letter-spacing: 0.06em;
  }

  .timer-display {
    font-variant-numeric: tabular-nums;
    font-size: clamp(42px, 8vw, 64px);
    font-weight: 800;
    line-height: 1;
    color: #1b3a4b;
    letter-spacing: 0.04em;
  }

  .timer-panel.warn .timer-display {
    color: #8a4b12;
  }

  .timer-panel.urgent .timer-display,
  .timer-panel.done .timer-display {
    color: var(--minus);
  }

  .timer-status {
    margin: 6px 0 0;
    font-size: 12px;
    color: var(--muted);
  }

  .timer-controls {
    display: grid;
    gap: 10px;
  }

  .timer-presets {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
  }

  .timer-presets .chip.timer-preset-on {
    background: var(--primary);
    color: #fff;
    border-color: var(--primary);
  }

  .timer-custom {
    display: flex;
    flex-wrap: wrap;
    align-items: end;
    gap: 8px;
  }

  .timer-custom label {
    display: grid;
    gap: 4px;
    font-size: 12px;
    color: var(--muted);
  }

  .timer-custom input {
    width: 64px;
    padding: 8px 10px;
    border: 1px solid var(--line);
    border-radius: 10px;
    font-size: 15px;
    font-variant-numeric: tabular-nums;
  }

  .timer-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
  }

  #btnTimer.tab-on {
    background: var(--primary);
    color: #fff;
    border-color: var(--primary);
  }

  @media (max-width: 720px) {
    .timer-panel {
      grid-template-columns: 1fr;
      margin: 0 10px 8px;
    }
  }

  .blackboard {
    display: none;
  }

  .group-bar {
    display: grid;
    gap: 8px;
    padding: 10px 12px;
    background: #f7f3ea;
    border: 1px dashed var(--line);
    border-radius: 12px;
  }

  .group-bar[hidden] {
    display: none !important;
  }

  .group-bar-row {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    align-items: center;
  }

  .group-bar .group-size-label,
  .group-bar .group-pick-label {
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 6px;
    margin: 0;
    font-size: 13px;
  }

  .group-bar input[type="number"],
  .group-bar select {
    min-height: 40px;
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 0 8px;
    background: #fff;
    color: var(--ink);
  }

  .group-bar input[type="number"] {
    width: 64px;
  }

  .group-bar select {
    min-width: 96px;
  }

  .group-bar .group-apply {
    justify-content: flex-start;
    font-size: 13px;
  }

  .group-rank-wrap {
    margin-bottom: 16px;
  }

  .group-rank-wrap h2 {
    margin-bottom: 4px;
  }

  #groupRoster button {
    border-left: 4px solid var(--group-color, var(--line));
  }

  .board {
    flex: 1;
    min-height: 280px;
    display: grid;
    gap: 8px;
    align-content: start;
    touch-action: pan-y;
  }

  .board.board-list {
    grid-template-columns: repeat(auto-fill, minmax(128px, 1fr));
    gap: 10px;
    padding: 4px 2px 8px;
    overflow: auto;
  }

  .board.board-list .seat {
    position: relative;
    min-height: 108px;
    aspect-ratio: auto;
  }

  .board.board-list .seat-card {
    position: relative;
    inset: auto;
    width: 100%;
    height: auto;
    min-height: 108px;
    cursor: pointer;
    touch-action: manipulation;
  }

  .board.board-list .seat-name {
    white-space: normal;
    overflow: visible;
    word-break: break-word;
    font-size: 15px;
  }

  .board.board-list .seat-no {
    font-size: 12px;
  }

  .board-group-block {
    grid-column: 1 / -1;
    display: grid;
    gap: 8px;
    padding: 10px;
    border-radius: 14px;
    background: rgba(22, 48, 60, 0.04);
    border: 1px solid var(--line);
  }

  .board-group-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    font-weight: 700;
    color: var(--ink);
  }

  .board-group-head .dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: var(--group-color, var(--accent));
    display: inline-block;
    margin-right: 6px;
  }

  .board-group-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(128px, 1fr));
    gap: 8px;
  }

  .board-group-grid .seat {
    position: relative;
    min-height: 108px;
  }

  .board-group-grid .seat-card {
    position: relative;
    inset: auto;
    width: 100%;
    height: auto;
    min-height: 108px;
    cursor: pointer;
  }

  .lab-board {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    grid-template-rows: repeat(2, minmax(150px, 1fr));
    align-content: stretch;
    gap: 10px;
  }

  .lab-zone {
    min-height: 150px;
    border: 2px solid var(--group-color, var(--line));
    border-radius: 16px;
    background: #fffdf8;
    box-shadow: inset 0 4px 0 var(--group-color, var(--line));
    padding: 8px;
    display: flex;
    flex-direction: column;
    gap: 8px;
  }

  .lab-zone.drop-target {
    background: #fff4e4;
    box-shadow: inset 0 4px 0 var(--group-color), 0 0 0 3px rgba(217, 133, 43, 0.28);
  }

  .lab-zone-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    font-weight: 700;
    font-size: 14px;
  }

  .lab-zone-seats {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(86px, 1fr));
    gap: 6px;
    flex: 1;
    align-content: start;
  }

  .lab-board .seat-card {
    position: relative;
    inset: auto;
    min-height: 68px;
  }

  body.view-lab #btnGroup {
    display: none;
  }

  .seat {
    min-height: 72px;
    border-radius: 14px;
    border: 2px dashed #e6dccb;
    background: rgba(255, 255, 255, 0.45);
    position: relative;
  }

  .exam-table .exam-chip {
    font-size: 12px;
    padding: 2px 6px;
  }

  .exam-table td {
    white-space: nowrap;
  }

  .seat.drop-target {
    border-color: var(--accent);
    background: #fff4e4;
  }

  .seat-card {
    position: absolute;
    inset: 0;
    border: 2px solid #eadfcb;
    border-radius: 14px;
    background: var(--seat);
    padding: 8px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    touch-action: none;
    user-select: none;
    cursor: grab;
  }

  .seat-exam {
    display: flex;
    flex-wrap: wrap;
    gap: 2px;
    margin-top: 2px;
  }

  .exam-chip,
  .exam-combo,
  .exam-rank {
    font-size: 10px;
    line-height: 1.2;
    border-radius: 6px;
    padding: 1px 4px;
    font-weight: 700;
    background: #eef2f4;
    color: #3d4a52;
  }

  .exam-chip.tone-a { background: #d8f0e0; color: #1f6b3a; }
  .exam-chip.tone-b { background: #e4eef8; color: #24557a; }
  .exam-chip.tone-c { background: #f8e0de; color: #8b2c28; }
  .exam-chip.tone-w { background: #f3ead4; color: #6b5318; }
  .exam-combo { background: #efe8dc; }
  .exam-rank { background: #e8e4f4; color: #4a3b78; }

  .seat-card.has-group {
    border-color: var(--group-color);
    box-shadow: inset 4px 0 0 var(--group-color);
  }

  .seat-card.group-picked {
    box-shadow: inset 4px 0 0 var(--group-color), 0 0 0 3px rgba(61, 107, 138, 0.28);
  }

  .seat-group {
    position: absolute;
    top: 6px;
    right: 6px;
    font-size: 10px;
    font-weight: 700;
    line-height: 1.2;
    color: #fff;
    background: var(--group-color);
    border-radius: 999px;
    padding: 2px 6px;
  }

  .seat-card.dragging {
    opacity: 0.35;
  }

  .seat-card.selected {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(27, 58, 75, 0.18);
  }

  .seat-card.winner {
    border-color: var(--accent);
    animation: winner-glow 0.7s ease-in-out 2;
    z-index: 2;
  }

  .seat-card.score-plus {
    animation: pop-plus 0.7s ease;
    box-shadow: 0 0 0 4px rgba(44, 122, 75, 0.4);
  }
  .seat-card.score-minus {
    animation: shake-minus 0.55s ease;
    box-shadow: 0 0 0 4px rgba(180, 65, 60, 0.4);
  }

  .seat-no { font-size: 12px; color: var(--muted); }
  .seat-name { font-size: 16px; font-weight: 700; line-height: 1.2; }
  .seat-score {
    align-self: flex-end;
    min-width: 28px;
    text-align: center;
    border-radius: 999px;
    padding: 2px 8px;
    font-size: 13px;
    background: #e8eef2;
  }

  .score-neg { background: #f8d4d2; color: var(--minus); }
  .score-mid { background: #d7efe2; color: var(--plus); }
  .score-high { background: #ffe3b8; color: #8a4b12; }

  .rank-list strong {
    min-width: 36px;
    text-align: center;
    border-radius: 999px;
    padding: 2px 8px;
    font-size: 14px;
  }

  .ghost {
    position: fixed;
    pointer-events: none;
    z-index: 30;
    width: 110px;
    min-height: 72px;
    border-radius: 14px;
    background: #fff;
    border: 2px solid var(--accent);
    padding: 8px;
    box-shadow: var(--shadow);
  }

  .side {
    min-height: 0;
    overflow: auto;
    padding: 14px;
  }

  .rank-panel {
    background:
      linear-gradient(180deg, rgba(27, 58, 75, 0.06), rgba(255, 253, 248, 0) 72px),
      #fffdf8;
  }

  .rank-hero h2 {
    font-size: 20px;
    letter-spacing: 0.02em;
  }

  .rank-hero .hint {
    margin-top: 6px;
    line-height: 1.45;
  }

  .roster {
    list-style: none;
    margin: 12px 0 0;
    padding: 0;
    display: grid;
    gap: 6px;
  }

  .rank-list-rich {
    gap: 8px;
  }

  .rank-list-rich button {
    padding: 10px 10px;
    gap: 10px;
    box-shadow: 0 4px 12px rgba(22, 48, 60, 0.06);
  }

  .rank-list-rich .rank-no {
    width: 34px;
    height: 34px;
    font-size: 15px;
  }

  .rank-list-rich .rank-delta {
    display: inline-flex;
    align-items: center;
    gap: 2px;
    margin-left: 6px;
    padding: 1px 6px;
    border-radius: 999px;
    font-size: 11px;
    font-weight: 700;
  }

  .rank-list-rich .rank-delta.up {
    background: #d7efe2;
    color: var(--plus);
  }

  .rank-list-rich .rank-delta.down {
    background: #f8d4d2;
    color: var(--minus);
  }

  .rank-list-rich .rank-delta.same {
    background: #e8eef2;
    color: var(--muted);
  }

  .rank-list-rich .rank-meta {
    display: flex;
    flex-wrap: wrap;
    gap: 4px 8px;
    align-items: center;
    white-space: normal;
    overflow: visible;
    text-overflow: unset;
  }

  .rank-chip {
    display: inline-block;
    padding: 1px 7px;
    border-radius: 999px;
    background: #eef3f6;
    color: var(--muted);
    font-size: 11px;
  }

  .rank-list-rich strong {
    min-width: 44px;
    font-size: 16px;
  }

  .roster button {
    width: 100%;
    text-align: left;
    min-height: 44px;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: #fff;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 8px 10px;
  }

  .roster button.selected {
    border-color: var(--primary);
    background: #eef4f7;
  }

  .rank-list .rank-no {
    width: 28px;
    height: 28px;
    border-radius: 999px;
    display: grid;
    place-items: center;
    font-size: 13px;
    font-weight: 800;
    background: #e8eef2;
    flex: 0 0 auto;
  }

  .rank-list .rank-main {
    flex: 1;
    min-width: 0;
    display: grid;
  }

  .rank-list .rank-name {
    font-weight: 700;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .rank-list .rank-meta {
    font-size: 12px;
    color: var(--muted);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .rank-list .gold .rank-no { background: #f3c84b; color: #5c3d00; }
  .rank-list .silver .rank-no { background: #cfd6de; color: #334155; }
  .rank-list .bronze .rank-no { background: #e1a078; color: #5c2e12; }
  .rank-list .rank-up { animation: rank-flash 0.8s ease; }

  .rank-empty {
    margin: 16px 0 0;
    padding: 18px 12px;
    text-align: center;
    color: var(--muted);
    border: 2px dashed var(--line);
    border-radius: 14px;
    font-size: 13px;
    line-height: 1.6;
  }

  .toast {
    position: fixed;
    left: 50%;
    bottom: calc(18px + var(--safe-bottom));
    transform: translateX(-50%);
    background: var(--ink);
    color: #fff;
    padding: 10px 16px;
    border-radius: 999px;
    z-index: 50;
  }

  .modal {
    position: fixed;
    inset: 0;
    background: rgba(22, 48, 60, 0.42);
    display: grid;
    place-items: center;
    align-content: center;
    padding: max(12px, env(safe-area-inset-top)) 12px max(12px, env(safe-area-inset-bottom));
    z-index: 40;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .modal[hidden], .toast[hidden], [hidden] {
    display: none !important;
  }

  .modal-card {
    width: min(520px, 100%);
    padding: 20px;
    display: grid;
    gap: 12px;
    max-height: none;
  }

  .modal-card.lottery-card {
    width: min(440px, 100%);
    max-height: min(92dvh, 920px);
    overflow-x: hidden;
    overflow-y: auto;
    gap: 8px;
    padding: 14px 14px 12px;
    align-self: center;
    margin: auto;
  }

  body:not(.teacher-on) .teacher-only {
    display: none !important;
  }

  .pw-mask {
    -webkit-text-security: disc;
    text-security: disc;
    letter-spacing: 0.2em;
    font-family: Tahoma, sans-serif;
  }

  .teacher-page {
    min-height: 100dvh;
    display: grid;
    grid-template-rows: auto auto 1fr;
    padding: calc(12px + var(--safe-top)) 12px calc(12px + var(--safe-bottom));
    gap: 10px;
  }

  .teacher-tabs .tab-on {
    background: var(--primary);
    color: #fff;
    border-color: var(--primary);
  }

  .teacher-page-main {
    min-height: 0;
    display: flex;
    flex-direction: column;
    gap: 12px;
    padding: 16px;
    background: #fffdf8;
    border: 1px solid var(--line);
    border-radius: 18px;
    box-shadow: var(--shadow);
    overflow: hidden;
  }

  .teacher-body {
    flex: 1;
    min-height: 0;
    overflow: hidden;
    display: flex;
    flex-direction: column;
  }

  .teacher-panel {
    flex: 1;
    min-height: 0;
    overflow: auto;
    display: flex;
    flex-direction: column;
    gap: 10px;
  }

  .teacher-panel .db-table-wrap {
    flex: 1;
  }

  .journal-overview {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
    gap: 8px;
  }

  .journal-card {
    display: grid;
    gap: 4px;
    text-align: left;
    padding: 10px 12px;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: #fff;
    cursor: pointer;
  }

  .journal-card span {
    color: var(--muted);
    font-size: 13px;
  }

  .journal-card strong {
    font-size: 16px;
    line-height: 1.35;
  }

  .journal-card em {
    font-style: normal;
    color: var(--muted);
    font-size: 12px;
  }

  .journal-card small {
    color: #5b7380;
    font-size: 12px;
    line-height: 1.35;
  }

  .journal-card.tab-on {
    border-color: var(--primary);
    box-shadow: inset 0 0 0 1px var(--primary);
  }

  .journal-latest-row td {
    background: #f3f8fb;
    font-weight: 600;
  }

  .journal-form {
    grid-template-columns: 1.6fr 0.8fr 1.2fr;
  }

  #tabDaily.teacher-panel {
    overflow: hidden;
  }

  #tabSummary.teacher-panel {
    overflow: auto;
  }

  #tabSummary > .hint {
    flex-shrink: 0;
  }

  .grade-fold {
    flex-shrink: 0;
    background: #fff;
    border: 1px solid var(--line);
    border-radius: 14px;
    padding: 0 12px 12px;
  }

  .grade-fold > summary,
  .grade-board > summary {
    cursor: pointer;
    list-style: none;
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 700;
  }

  .grade-fold > summary {
    font-size: 16px;
    padding: 12px 4px;
  }

  .grade-board > summary {
    font-size: 14px;
    padding: 10px 8px;
  }

  .grade-fold > summary::-webkit-details-marker,
  .grade-board > summary::-webkit-details-marker,
  .rule-details summary::-webkit-details-marker {
    display: none;
  }

  .grade-fold > summary::before,
  .grade-board > summary::before,
  .rule-details summary::before {
    content: "▸";
    width: 1em;
    color: var(--muted);
    font-size: 14px;
  }

  .grade-fold[open] > summary::before,
  .grade-board[open] > summary::before,
  .rule-details[open] > summary::before {
    content: "▾";
  }

  .grade-fold > summary + *,
  .grade-fold[open] > .grade-toolbar {
    margin-top: 4px;
  }

  .sheet-table th small,
  .sheet-table th .col-title {
    display: block;
  }

  .sheet-table th small {
    font-weight: 400;
    color: var(--muted);
    font-size: 11px;
  }

  .sheet-table th .col-title {
    font-weight: 700;
  }

  #tabSummary.teacher-panel .db-table-wrap {
    flex: 1;
  }

  .grade-subtabs {
    display: none;
  }

  .grade-toolbar {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    align-items: end;
    flex-shrink: 0;
    margin: 0 0 10px;
  }

  .grade-toolbar label {
    display: grid;
    gap: 4px;
    min-width: 120px;
    font-size: 12px;
    color: var(--muted);
  }

  .grade-toolbar input,
  .grade-toolbar select {
    min-height: 40px;
    min-width: 0;
    padding: 8px 10px;
    border-radius: 10px;
  }

  .grade-toolbar select {
    min-width: 168px;
  }

  .grade-boards {
    display: flex;
    flex-direction: column;
    gap: 8px;
  }

  .grade-board {
    border: 1px solid var(--line);
    border-radius: 12px;
    background: #faf8f3;
    overflow: hidden;
  }

  .grade-board > summary {
    background: #fff;
    border-left: 4px solid var(--primary);
  }

  .grade-board.tone-usual > summary { border-left-color: #2c7a4b; }
  .grade-board.tone-quiz > summary { border-left-color: #d9852b; }
  .grade-board.tone-exam > summary { border-left-color: #b4413c; }
  .grade-board.tone-sum > summary { border-left-color: #1b3a4b; }

  .grade-board .sheet-wrap {
    flex: none;
    min-height: 0;
    margin: 0 8px 8px;
    max-height: min(56vh, 520px);
  }

  .empty-board {
    margin: 0;
    padding: 28px 12px;
    text-align: center;
    color: var(--muted);
    background: #fff;
    border: 1px dashed var(--line);
    border-radius: 12px;
  }

  .rule-box {
    flex-shrink: 0;
    background: #fff;
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 8px 14px;
    font-size: 14px;
    line-height: 1.7;
  }

  .rule-details summary {
    cursor: pointer;
    font-weight: 700;
    font-size: 15px;
    list-style: none;
  }

  .rule-details summary::-webkit-details-marker {
    display: none;
  }

  .rule-details ol {
    margin: 8px 0 0;
    padding-left: 22px;
    color: var(--ink);
  }

  .rule-box .rule-note {
    margin: 8px 0 0;
    color: var(--muted);
    font-size: 13px;
  }

  .col-del-mini {
    border: 0;
    background: none;
    color: var(--minus);
    cursor: pointer;
    font-size: 12px;
    padding: 0 4px;
    min-height: 0;
    min-width: 0;
  }

  .report-table input,
  .report-table select {
    min-height: 32px;
    padding: 4px 6px;
    border-radius: 8px;
    font-size: 13px;
  }

  .cell-row {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
  }

  .cell-stack {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 4px;
    min-width: 118px;
  }

  .mini-num {
    width: 58px;
    text-align: center;
  }

  .mini-date {
    width: 100%;
    min-width: 0;
  }

  .leave-toggle {
    display: inline-flex;
    align-items: center;
    gap: 2px;
    font-size: 12px;
    color: var(--muted);
    white-space: nowrap;
    margin: 0;
  }

  .leave-toggle input {
    width: 16px;
    min-height: 16px;
    padding: 0;
    accent-color: var(--minus);
  }

  .hw-cell[data-submitted="0"] .hw-detail {
    display: none;
  }

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

  .hw-final {
    display: block;
    font-size: 12px;
    color: var(--muted);
    white-space: normal;
    line-height: 1.35;
  }

  .hw-final.is-late {
    color: var(--minus);
    font-weight: 700;
  }

  .sheet-table input[type="number"] {
    width: 58px;
    min-height: 32px;
    padding: 4px 6px;
    text-align: center;
  }

  .report-table td,
  .report-table th {
    text-align: center;
  }

  .report-table .name-col {
    text-align: left;
  }

  .report-table td.grade-cell,
  .report-table td.hw-cell,
  .report-table td.formula-col {
    white-space: normal;
  }

  .report-table td.formula-col {
    text-align: left;
    min-width: 220px;
    max-width: 340px;
    font-size: 12px;
    line-height: 1.45;
    color: var(--muted);
  }

  #gradeWeekView .sheet-wrap {
    flex: none;
    max-height: min(56vh, 520px);
    margin-top: 4px;
  }

  .grade-toolbar .tool {
    min-height: 40px;
  }

  #tabStats.teacher-panel {
    overflow: auto;
  }

  a.tool {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    text-decoration: none;
  }

  .timetable-panel {
    overflow: auto;
    gap: 12px;
  }

  .tt-head {
    display: flex;
    flex-wrap: wrap;
    gap: 10px 16px;
    align-items: flex-end;
    justify-content: space-between;
    flex-shrink: 0;
  }

  .tt-head-copy {
    display: flex;
    flex-direction: column;
    gap: 2px;
    min-width: 200px;
  }

  .tt-head-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    align-items: center;
  }

  .tt-head-actions .tool {
    min-height: 40px;
    font-size: 13px;
  }

  .tt-sheets {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    align-items: center;
  }

  .tt-sheets:empty,
  .tt-sheets[hidden] {
    display: none;
  }

  .tt-clock {
    margin: 0;
    font-size: 18px;
    font-weight: 800;
    letter-spacing: 0.02em;
  }

  .tt-head .hint {
    margin: 0;
  }

  .tt-focus {
    flex-shrink: 0;
  }

  .tt-focus-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    align-items: stretch;
  }

  .tt-focus-card {
    display: grid;
    grid-template-columns: 1fr auto;
    grid-template-rows: auto auto auto;
    gap: 4px 12px;
    padding: 12px 14px;
    border-radius: 14px;
    min-height: 96px;
    align-items: center;
  }

  .tt-focus-now {
    background: #eaf4fb;
    border: 2px solid var(--primary);
  }

  .tt-focus-next {
    background: #fff7ec;
    border: 2px dashed var(--accent);
  }

  .tt-kicker {
    margin: 0;
    font-size: 12px;
    font-weight: 700;
    letter-spacing: 0.1em;
    color: var(--muted);
    grid-column: 1 / -1;
    grid-row: 1;
  }

  .tt-period {
    margin: 0;
    font-size: 15px;
    font-weight: 800;
    line-height: 1.3;
    grid-column: 1 / -1;
    grid-row: 2;
    word-break: break-word;
  }

  .tt-time {
    display: none;
  }

  .tt-focus-body {
    min-height: 40px;
    display: flex;
    align-items: center;
    gap: 8px;
    grid-column: 1;
    grid-row: 3;
    overflow: visible;
  }

  .tt-focus-journal {
    display: none;
  }

  .tt-remain {
    display: none;
  }

  .tt-journal {
    margin: 0;
    display: grid;
    gap: 2px;
    line-height: 1.35;
  }

  .tt-journal-date {
    font-size: 12px;
    color: var(--muted);
  }

  .tt-journal-progress {
    font-size: 15px;
    font-weight: 700;
    color: var(--ink);
  }

  .tt-journal-note {
    font-size: 13px;
    color: #5b7380;
  }

  .tt-journal-empty {
    margin: 0;
    font-size: 13px;
    color: var(--muted);
  }

  .tt-focus-item,
  .tt-focus-body .tt-class {
    font-size: 26px;
    min-width: 64px;
    min-height: 40px;
    padding: 6px 12px;
    line-height: 1.2;
  }

  .tt-focus-empty {
    color: var(--muted);
    font-size: 15px;
    font-weight: 600;
  }

  .tt-focus-next .tt-remain {
    color: #b56a16;
  }

  .tt-focus-action {
    min-height: 40px;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    grid-column: 2;
    grid-row: 3;
  }

  .tt-focus-action .tool {
    min-height: 40px;
    padding: 6px 12px;
    font-size: 14px;
  }

  .tt-holiday-banner {
    margin: 8px 0 0;
    padding: 8px 12px;
    border-radius: 10px;
    background: #fde8e8;
    border: 1px solid #e8b4b4;
    color: #7a2e2e;
    font-size: 13px;
    line-height: 1.45;
  }

  .tt-holiday-banner[hidden] {
    display: none;
  }

  .tt-table th.tt-holiday-day,
  .tt-table td.tt-holiday-day {
    background: #fdecec;
  }

  .tt-table th.tt-holiday-day {
    color: #9b3030;
  }

  .tt-table td.tt-holiday-day {
    color: #9b3030;
    font-weight: 700;
  }

  .tt-swap {
    flex-shrink: 0;
    margin: 12px 0;
    padding: 14px 16px;
    border: 1px solid #d7e0ea;
    border-radius: 14px;
    background: linear-gradient(180deg, #fff9f0 0%, #ffffff 70%);
  }

  .tt-swap-head h3 {
    margin: 0 0 4px;
    font-size: 1.05rem;
  }

  .tt-swap-head .hint {
    margin: 0 0 12px;
  }

  .tt-swap-form {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
    gap: 10px 12px;
    align-items: end;
    margin-bottom: 12px;
  }

  .tt-swap-form label {
    display: grid;
    gap: 4px;
    font-size: 13px;
    color: #445;
  }

  .tt-swap-form select,
  .tt-swap-form input {
    min-height: 36px;
    padding: 6px 8px;
    border: 1px solid #c9d4e0;
    border-radius: 8px;
    background: #fff;
    font: inherit;
  }

  .tt-swap-note {
    grid-column: span 2;
  }

  .tt-swap-list {
    display: grid;
    gap: 8px;
  }

  .tt-swap-item {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px 12px;
    padding: 10px 12px;
    border-radius: 10px;
    background: #fff;
    border: 1px solid #ead9c4;
  }

  .tt-swap-item strong {
    color: #9a4b00;
  }

  .tt-swap-item .tt-swap-meta {
    flex: 1 1 220px;
    color: #334;
    font-size: 14px;
  }

  .tt-swap-empty {
    margin: 0;
    color: #667;
    font-size: 14px;
  }

  .tt-table td.tt-swapped {
    background: #fff3d6;
    box-shadow: inset 0 0 0 2px #e0a44a;
  }

  .tt-table td.tt-swapped .tt-badge-swap {
    background: #c56a00;
  }

  .tt-wrap,
  .tt-table-wrap {
    overflow: auto;
    -webkit-overflow-scrolling: touch;
    background: #fff;
    border: 1px solid var(--line);
    border-radius: 14px;
  }

  .tt-table {
    width: 100%;
    min-width: 720px;
    border-collapse: separate;
    border-spacing: 0;
    table-layout: fixed;
    font-size: 14px;
  }

  .tt-col-period { width: 72px; }
  .tt-col-time { width: 108px; }

  .tt-table th,
  .tt-table td {
    border-bottom: 1px solid #eee4d6;
    border-right: 1px solid #f3ebe0;
    padding: 8px 6px;
    text-align: center;
    vertical-align: middle;
    height: 48px;
  }

  .tt-table thead th {
    background: #f7f1e6;
    font-weight: 700;
    position: sticky;
    top: 0;
    z-index: 4;
  }

  .tt-table .tt-col-period {
    position: sticky;
    left: 0;
    z-index: 3;
    background: #fbf7f0;
    font-weight: 700;
  }

  .tt-table thead .tt-col-period {
    z-index: 5;
  }

  .tt-table .tt-col-time {
    position: sticky;
    left: 72px;
    z-index: 3;
    color: var(--muted);
    background: #fff8f0;
    white-space: nowrap;
    font-variant-numeric: tabular-nums;
  }

  .tt-table thead .tt-col-time {
    z-index: 5;
    color: var(--ink);
  }

  .tt-table .tt-today {
    background: #eef6f1;
  }

  .tt-table thead .tt-today {
    background: #cfe6d4;
  }

  .tt-table td.tt-current,
  .tt-table td.tt-upcoming {
    position: relative;
    padding-top: 18px;
  }

  .tt-table td.tt-current {
    background: #d5eaf6;
    outline: 3px solid var(--primary);
    outline-offset: -3px;
    z-index: 2;
  }

  .tt-table td.tt-upcoming {
    background: #fff1dc;
    outline: 3px dashed var(--accent);
    outline-offset: -3px;
    z-index: 1;
  }

  .tt-badge {
    position: absolute;
    top: 3px;
    left: 50%;
    transform: translateX(-50%);
    padding: 1px 7px;
    border-radius: 999px;
    background: var(--primary);
    color: #fff;
    font-size: 11px;
    font-weight: 700;
    line-height: 1.35;
    white-space: nowrap;
  }

  .tt-upcoming .tt-badge {
    background: var(--accent);
  }

  .tt-table tr.tt-break td,
  .tt-table tr.tt-break th {
    background: #f6f2ea;
    color: var(--muted);
    font-size: 12px;
    height: 36px;
  }

  .tt-table tr.tt-break .tt-col-period,
  .tt-table tr.tt-break .tt-col-time {
    background: #f6f2ea;
  }

  .tt-class {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 52px;
    min-height: 32px;
    padding: 4px 10px;
    border: 0;
    border-radius: 10px;
    font-weight: 700;
    color: var(--ink);
    cursor: pointer;
  }

  .tt-live {
    flex-shrink: 0;
    background: #fff;
    border: 1px solid var(--line);
    border-radius: 14px;
    padding: 4px 14px 12px;
  }

  .tt-live summary {
    cursor: pointer;
    font-weight: 700;
    padding: 10px 0;
    color: var(--muted);
  }

  .tt-embed {
    display: block;
    width: 100%;
    min-height: 420px;
    border: 1px solid var(--line);
    border-radius: 10px;
    background: #fff;
  }

  .stats-switch {
    display: flex;
    gap: 8px;
    flex-shrink: 0;
  }

  .stats-kind-switch {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    flex-shrink: 0;
  }

  .stats-wide tbody tr[data-seat],
  .stats-wide tbody tr[data-class] {
    cursor: pointer;
  }

  .stats-wide tbody tr[data-seat]:hover td,
  .stats-wide tbody tr[data-class]:hover td {
    background: #eef6fb;
  }

  .person-toolbar {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    align-items: end;
    background: #fff;
    border: 1px solid var(--line);
    border-radius: 14px;
    padding: 10px 12px;
  }

  .person-toolbar label {
    min-width: 220px;
    flex: 1;
  }

  .person-nav {
    display: flex;
    gap: 8px;
  }

  #statsClassView,
  #statsSchoolView,
  #statsPersonView {
    display: flex;
    flex-direction: column;
    gap: 10px;
  }

  #statsClassView[hidden],
  #statsSchoolView[hidden],
  #statsPersonView[hidden] {
    display: none !important;
  }

  .stats-watch {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 10px;
  }

  .watch-card {
    background: #fff;
    border: 1px solid var(--line);
    border-radius: 14px;
    padding: 12px 14px;
  }

  .watch-card h4 {
    margin: 0 0 6px;
    font-size: 12px;
    letter-spacing: 0.04em;
    color: var(--muted);
  }

  .watch-card p {
    margin: 0;
    font-size: 15px;
    line-height: 1.45;
  }

  .watch-card.tone-good {
    background: #f1f8f3;
    border-color: #cfe6d6;
  }

  .watch-card.tone-warn {
    background: #fff7ec;
    border-color: #f0d7b0;
  }

  .watch-card.tone-alert {
    background: #fdf2f1;
    border-color: #f0c7c4;
  }

  .watch-card.tone-info {
    background: #f3f8fb;
    border-color: #cfe0ea;
  }

  .stats-block {
    background: #fff;
    border: 1px solid var(--line);
    border-radius: 14px;
    padding: 12px 14px 14px;
  }

  .group-deduct-box {
    margin-top: 12px;
  }

  .deduct-sub {
    margin: 14px 0 8px;
    font-size: 14px;
  }

  .deduct-log {
    margin: 0;
    padding-left: 1.2em;
    display: grid;
    gap: 8px;
    font-size: 14px;
    line-height: 1.45;
  }

  .deduct-summary-wrap {
    margin-top: 8px;
  }

  .stats-block h3 {
    margin: 0 0 10px;
    font-size: 16px;
  }

  .chart-sub {
    margin: 14px 0 8px;
    font-size: 13px;
    color: #5b7380;
    font-weight: 700;
  }

  .heat-wrap {
    overflow: auto;
    max-height: 420px;
  }

  .heat-svg {
    min-width: 100%;
  }

  .assess-wrap {
    overflow: auto;
    margin-bottom: 10px;
  }

  .assess-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;
  }

  .assess-table th,
  .assess-table td {
    border-bottom: 1px solid #eee4d6;
    padding: 8px 10px;
    text-align: left;
    white-space: nowrap;
  }

  .assess-table th {
    color: var(--muted);
    font-weight: 700;
    background: #f7f3ec;
  }

  .stats-table-wrap {
    max-height: min(52vh, 560px) !important;
  }

  .stats-wide {
    min-width: 960px;
  }

  .stats-panel .db-table-wrap {
    flex: 0 0 auto;
    max-height: 360px;
  }

  .teacher-footer {
    display: flex;
    justify-content: flex-end;
    flex-shrink: 0;
  }

  .settings-panel textarea {
    min-height: 220px;
  }

  .cloud-box {
    background: #fff;
    border: 1px solid var(--line);
    border-radius: 14px;
    padding: 14px 16px 16px;
    margin-bottom: 14px;
  }

  .cloud-box h3 {
    margin: 0 0 6px;
    font-size: 16px;
  }

  .cloud-status {
    margin: 8px 0;
    font-weight: 700;
    color: #1b3a4b;
  }

  .cloud-steps {
    margin: 8px 0 12px 1.2em;
    padding: 0;
    color: var(--muted);
    font-size: 13px;
    line-height: 1.55;
  }

  .cloud-box label {
    display: block;
    margin: 8px 0;
  }

  .cloud-box input[type="url"] {
    width: 100%;
    margin-top: 4px;
  }

  .subhead {
    margin: 4px 0 0;
    font-size: 15px;
  }

  .stat-cards {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
    gap: 8px;
  }

  .stat-card {
    background: #fff;
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 10px;
  }

  .stat-card span {
    display: block;
    color: var(--muted);
    font-size: 12px;
  }

  .stat-card strong {
    font-size: 22px;
  }

  .sheet-wrap {
    flex: 1;
    min-height: 240px;
    overflow: auto;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: #fff;
  }

  .sheet-table {
    width: max-content;
    min-width: 100%;
    border-collapse: collapse;
    font-size: 13px;
  }

  .sheet-table th,
  .sheet-table td {
    border: 1px solid #e4dbcc;
    padding: 6px 8px;
    text-align: center;
    white-space: nowrap;
  }

  .sheet-table th {
    position: sticky;
    top: 0;
    z-index: 2;
    background: #eef4f7;
    font-weight: 700;
    color: var(--muted);
  }

  .grade-board.tone-usual .sheet-table th,
  .grade-board.tone-usual .sheet-table th.sticky-1,
  .grade-board.tone-usual .sheet-table th.sticky-2 { background: #eaf6ee; }
  .grade-board.tone-quiz .sheet-table th,
  .grade-board.tone-quiz .sheet-table th.sticky-1,
  .grade-board.tone-quiz .sheet-table th.sticky-2 { background: #fff4dc; }
  .grade-board.tone-exam .sheet-table th,
  .grade-board.tone-exam .sheet-table th.sticky-1,
  .grade-board.tone-exam .sheet-table th.sticky-2 { background: #fdecea; }
  .grade-board.tone-sum .sheet-table th,
  .grade-board.tone-sum .sheet-table th.sticky-1,
  .grade-board.tone-sum .sheet-table th.sticky-2 { background: #e8eef2; }

  .sheet-table tfoot td {
    position: sticky;
    bottom: 0;
    background: #f7f1e6;
    font-weight: 700;
  }

  .sheet-table .sticky-1,
  .sheet-table .sticky-2 {
    position: sticky;
    background: #fff;
    z-index: 1;
  }

  .sheet-table th.sticky-1,
  .sheet-table th.sticky-2 {
    z-index: 4;
    background: #eef4f7;
  }

  .sheet-table tfoot .sticky-1,
  .sheet-table tfoot .sticky-2 {
    z-index: 3;
    background: #f7f1e6;
  }

  .sheet-table .sticky-1 { left: 0; min-width: 52px; }
  .sheet-table .sticky-2 { left: 52px; min-width: 88px; text-align: left; }

  .sheet-table th[data-day],
  .sheet-table td[data-day] { cursor: pointer; min-width: 64px; }
  .sheet-table .col-on { background: #e7f3fb; }
  .sheet-table th.col-on { background: #d5eaf6; color: var(--primary); }
  .sheet-table .col-total { background: #fff8e8; font-weight: 700; }
  .sheet-table th.col-total { background: #ffe9c4; color: var(--ink); }
  .sheet-table .day-plus { color: var(--plus); }
  .sheet-table .day-minus { color: var(--minus); }
  .sheet-table tbody tr:hover td { background: #f4f8fb; }
  .sheet-table tbody tr:hover .sticky-1,
  .sheet-table tbody tr:hover .sticky-2 { background: #f4f8fb; }

  .insight-list {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 8px;
  }

  .insight-item {
    background: #fff;
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 10px 12px;
    font-size: 14px;
  }

  .insight-item span {
    display: block;
    color: var(--muted);
    font-size: 12px;
  }

  .chart-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
  }

  .chart-wide {
    grid-column: 1 / -1;
  }

  .chart-card {
    margin: 0;
    background: #fff;
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 10px 12px 6px;
  }

  .chart-card figcaption {
    font-size: 13px;
    font-weight: 700;
    margin-bottom: 6px;
  }

  .chart-svg {
    width: 100%;
    height: auto;
    display: block;
  }

  .chart-empty {
    color: var(--muted);
    font-size: 13px;
    padding: 24px 8px;
    text-align: center;
  }

  .db-toolbar {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    align-items: end;
    flex-shrink: 0;
  }

  .db-toolbar label {
    min-width: 160px;
    flex: 1;
  }

  .date-picker {
    flex: 2;
    min-width: 280px;
    display: grid;
    gap: 6px;
    font-size: 14px;
  }

  .date-nav {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    align-items: center;
  }

  .date-nav input[type="date"] {
    min-height: 44px;
    min-width: 150px;
  }

  .score-day-label {
    flex: 1 0 100%;
    margin: 0;
    font-size: 16px;
    font-weight: 700;
    color: var(--primary);
  }

  .db-table tr.day-on td {
    background: #e7f3fb;
  }

  .db-table tr[data-day] {
    cursor: pointer;
  }

  .db-table .day-plus { color: var(--plus); font-weight: 700; }
  .db-table .day-minus { color: var(--minus); font-weight: 700; }

  .db-table-wrap {
    overflow: auto;
    min-height: 0;
    border: 1px solid var(--line);
    border-radius: 14px;
    background: #fff;
  }

  .db-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 15px;
  }

  .db-table th {
    position: sticky;
    top: 0;
    background: #eef4f7;
    text-align: left;
    padding: 10px 8px;
    font-size: 13px;
    color: var(--muted);
  }

  .db-table td {
    padding: 4px 6px;
    border-top: 1px solid var(--line);
  }

  .db-table input {
    width: 100%;
    min-height: 40px;
    border-radius: 8px;
    padding: 8px;
  }

  .db-table .col-class { width: 18%; }
  .db-table .col-seat { width: 16%; }
  .db-table .col-name { width: 38%; }
  .db-table .col-score { width: 16%; }
  .db-table .col-del { width: 12%; text-align: right; }

  .db-table .tool.danger {
    min-width: 40px;
    padding: 0 10px;
    color: var(--minus);
  }

  .upload-box {
    border: 2px dashed var(--line);
    border-radius: 14px;
    padding: 14px;
    background: rgba(255, 255, 255, 0.7);
    text-align: center;
  }

  .upload-box.dragover {
    border-color: var(--accent);
    background: #fff4e4;
  }

  .roster-upload-actions {
    justify-content: center;
    flex-wrap: wrap;
    margin-top: 10px;
  }
  .upload-box p { margin: 0 0 6px; }
  .upload-preview {
    margin-top: 8px !important;
    color: var(--plus);
    font-weight: 700;
  }

  button.linkish {
    border: 0;
    background: none;
    color: var(--primary);
    text-decoration: underline;
    padding: 0;
    min-height: 0;
    cursor: pointer;
  }

  .check.left {
    justify-content: flex-start;
    text-align: left;
    align-items: flex-start;
    font-size: 13px;
    color: var(--muted);
  }

  .lottery-card { text-align: center; }
  .lottery-head {
    display: grid;
    gap: 2px;
    justify-items: center;
  }
  .lottery-sub {
    margin: 0;
    font-size: 12px;
    color: var(--muted);
    letter-spacing: 0.08em;
  }
  .lottery-options {
    display: grid;
    gap: 8px;
    text-align: left;
  }
  .lottery-tiers,
  .lottery-modes {
    margin: 0;
    padding: 10px 12px;
    border-radius: 14px;
    background: linear-gradient(180deg, #fff, #f7f3ea);
    border: 1px solid var(--line);
    text-align: left;
  }
  .lottery-tiers-label {
    margin: 0 0 8px;
    font-size: 12px;
    color: var(--muted);
    letter-spacing: 0.04em;
  }
  .lottery-pills {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
  }
  .lottery-pill {
    position: relative;
    margin: 0;
    cursor: pointer;
  }
  .lottery-pill input {
    position: absolute;
    opacity: 0;
    pointer-events: none;
  }
  .lottery-pill span {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 52px;
    min-height: 34px;
    padding: 0 12px;
    border-radius: 999px;
    border: 1px solid var(--line);
    background: #fff;
    color: var(--ink);
    font-size: 13px;
    font-weight: 600;
  }
  .lottery-pill input:checked + span {
    background: var(--primary);
    border-color: var(--primary);
    color: #fff;
  }
  .lottery-unique {
    justify-content: flex-start;
    font-size: 13px;
    margin: 0;
  }
  .lottery-tier-hint {
    margin: 8px 0 0;
    font-size: 11px;
    color: var(--muted);
  }
  .lottery-fate {
    margin: 0;
    padding: 12px;
    border-radius: 14px;
    background:
      radial-gradient(circle at 20% 0%, rgba(243, 200, 75, 0.35), transparent 55%),
      linear-gradient(180deg, #fff8e8, #f7f3ea);
    border: 1px solid #ebd39a;
    text-align: center;
  }
  .lottery-fate[hidden] { display: none !important; }
  .lottery-fate-label {
    margin: 0;
    font-size: 20px;
    font-weight: 800;
    color: #8a5a00;
  }
  .lottery-fate-hint {
    margin: 4px 0 8px;
    font-size: 10px;
    color: var(--muted);
  }
  .lottery-fate-actions {
    display: flex;
    justify-content: center;
    gap: 8px;
    flex-wrap: wrap;
  }

  .fate-panel {
    position: fixed;
    inset: 0;
    z-index: 70;
    display: grid;
    place-items: center;
    padding: max(12px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom));
    background: rgba(22, 48, 60, 0.28);
    pointer-events: auto;
  }

  .fate-panel[hidden] {
    display: none !important;
  }

  .fate-panel-card {
    pointer-events: auto;
    width: min(380px, 100%);
    padding: 16px 18px;
    border-radius: 16px;
    text-align: center;
    border: 1px solid #ebd39a;
    background:
      radial-gradient(circle at 20% 0%, rgba(243, 200, 75, 0.4), transparent 55%),
      linear-gradient(180deg, #fff8e8, #f7f3ea);
    box-shadow: 0 16px 40px rgba(27, 58, 75, 0.28);
  }

  .fate-panel-kicker {
    margin: 0;
    font-size: 12px;
    font-weight: 700;
    letter-spacing: 0.08em;
    color: #8a5a00;
  }

  .fate-panel-who {
    margin: 4px 0 0;
    font-size: 14px;
    font-weight: 700;
    color: #1b3a4b;
  }

  .fate-panel-label {
    margin: 6px 0 0;
    font-size: 24px;
    font-weight: 800;
    color: #8a5a00;
  }

  .fate-panel-hint {
    margin: 4px 0 12px;
    font-size: 12px;
    color: var(--muted);
    line-height: 1.45;
  }

  .fate-panel-actions {
    display: flex;
    justify-content: center;
    gap: 8px;
    flex-wrap: wrap;
  }

  .fate-panel-actions .tool {
    min-width: 96px;
    min-height: 44px;
  }

  .seat-card.peer-helper {
    box-shadow: 0 0 0 3px #2f6f8f, 0 8px 18px rgba(47, 111, 143, 0.35);
  }
  .tool.active-peer {
    background: #2f6f8f;
    color: #fff;
    border-color: #2f6f8f;
  }
  .lottery-reel {
    margin: 0;
    padding: 18px 8px 14px;
    border-radius: 18px;
    background:
      radial-gradient(circle at 50% 18%, #3d7a95, transparent 42%),
      radial-gradient(circle at 50% 30%, #2a5368, #122832 70%);
    box-shadow:
      inset 0 0 0 2px rgba(126, 182, 214, 0.4),
      0 12px 28px rgba(18, 40, 50, 0.28);
    position: relative;
  }
  .lottery-stamp {
    position: absolute;
    top: 8px;
    right: 10px;
    width: 40px;
    height: 40px;
    border: 3px solid #ff5b5b;
    color: #ff5b5b;
    border-radius: 50%;
    display: grid;
    place-items: center;
    font-weight: 900;
    font-size: 16px;
    transform: rotate(18deg);
    background: rgba(255, 255, 255, 0.12);
  }
  .lottery-stamp[hidden] { display: none !important; }
  .lottery-card.revealed .lottery-stamp {
    animation: stamp-in 0.28s cubic-bezier(0.2, 1.4, 0.3, 1);
  }
  .lottery-name {
    font-size: clamp(28px, 6vw, 38px);
    font-weight: 800;
    min-height: 44px;
    color: #fff8e8;
    text-shadow: 0 6px 18px rgba(0, 0, 0, 0.35);
  }
  .lottery-card .modal-kicker {
    margin: 0;
    font-size: 13px;
    letter-spacing: 0.18em;
    text-transform: uppercase;
    color: var(--muted);
  }
  .lottery-card .lottery-meta {
    margin: 0;
    font-size: 12px;
  }
  .lottery-card .modal-actions {
    position: sticky;
    bottom: 0;
    padding-top: 8px;
    background: linear-gradient(180deg, rgba(255, 253, 248, 0), #fffdf8 28%);
    justify-content: center;
  }
  .lottery-card .check {
    font-size: 13px;
  }
  .lottery-card.rolling .lottery-name {
    animation: name-flicker 0.08s linear infinite;
  }
  .lottery-card.revealed .lottery-reel {
    animation: flash-reveal 0.45s ease;
    box-shadow: inset 0 0 0 3px #7eb6d6, 0 0 24px rgba(126, 182, 214, 0.55);
  }
  .lottery-card.revealed .lottery-name {
    color: #fff;
  }

  .spark, .spark-ring {
    position: fixed;
    pointer-events: none;
    z-index: 57;
  }
  .spark {
    width: 8px;
    height: 8px;
    margin: -4px 0 0 -4px;
    border-radius: 50%;
    animation: spark-out 0.55s ease-out forwards;
  }
  .spark-ring {
    width: 18px;
    height: 18px;
    margin: -9px 0 0 -9px;
    border: 3px solid rgba(126, 182, 214, 0.85);
    border-radius: 50%;
    animation: ring-out 0.55s ease-out forwards;
  }

  .fx-layer {
    position: fixed;
    inset: 0;
    pointer-events: none;
    z-index: 55;
    overflow: hidden;
  }

  .score-float {
    position: fixed;
    font-size: 34px;
    font-weight: 900;
    pointer-events: none;
    animation: float-score 1.05s ease-out forwards;
    text-shadow: 0 8px 16px rgba(22, 48, 60, 0.25);
    z-index: 56;
  }
  .score-float.plus { color: var(--plus); }
  .score-float.minus { color: var(--minus); }

  .confetti {
    position: absolute;
    top: -12px;
    width: 10px;
    height: 16px;
    border-radius: 2px;
    animation: confetti-fall linear forwards;
  }

  .form-grid {
    display: grid;
    grid-template-columns: 1.4fr 0.8fr 0.8fr;
    gap: 8px;
  }

  .empty-state {
    grid-column: 1 / -1;
    min-height: 220px;
    display: grid;
    place-items: center;
    text-align: center;
    gap: 12px;
    color: var(--muted);
    border: 2px dashed var(--line);
    border-radius: 16px;
    padding: 24px;
    background: rgba(255, 255, 255, 0.6);
  }

  label { display: grid; gap: 6px; font-size: 14px; }
  .check {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
  }

  .modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    flex-wrap: wrap;
  }

  @keyframes pulse {
    50% { transform: scale(1.04); background: #fff3dd; }
  }

  @keyframes pop-plus {
    0% { transform: scale(1); background: #fffdf8; }
    35% { transform: scale(1.08); background: #d7efe2; }
    100% { transform: scale(1); background: #fffdf8; }
  }

  @keyframes shake-minus {
    0%, 100% { transform: translateX(0); background: #fffdf8; }
    20% { transform: translateX(-6px); background: #f8d4d2; }
    40% { transform: translateX(6px); background: #f8d4d2; }
    60% { transform: translateX(-4px); }
    80% { transform: translateX(4px); }
  }

  @keyframes winner-glow {
    0%, 100% { transform: scale(1); background: #fffdf8; }
    50% { transform: scale(1.06); background: #e7f3fb; box-shadow: 0 0 0 6px rgba(126, 182, 214, 0.35); }
  }

  @keyframes float-score {
    0% { transform: translate(-50%, 0) scale(0.6); opacity: 0; }
    20% { transform: translate(-50%, -12px) scale(1.15); opacity: 1; }
    100% { transform: translate(-50%, -78px) scale(1); opacity: 0; }
  }

  @keyframes name-flicker {
    0% { opacity: 0.35; filter: blur(2px); }
    100% { opacity: 1; filter: blur(0); }
  }

  @keyframes flash-reveal {
    0% { transform: scale(0.94); filter: brightness(2); }
    100% { transform: scale(1); filter: brightness(1); }
  }

  @keyframes stamp-in {
    0% { transform: rotate(18deg) scale(1.8); opacity: 0; }
    100% { transform: rotate(18deg) scale(1); opacity: 1; }
  }

  @keyframes spark-out {
    0% { transform: translate(0, 0) scale(1); opacity: 1; }
    100% { transform: translate(var(--dx), var(--dy)) scale(0.2); opacity: 0; }
  }

  @keyframes ring-out {
    0% { transform: scale(0.4); opacity: 0.9; }
    100% { transform: scale(8); opacity: 0; }
  }

  @keyframes confetti-fall {
    0% { transform: translateY(0) rotate(0deg); opacity: 1; }
    100% { transform: translateY(110vh) rotate(540deg); opacity: 0; }
  }

  @keyframes rank-flash {
    0% { background: #fff3dd; transform: scale(1.03); }
    100% { background: #fff; transform: scale(1); }
  }

  @media (max-width: 900px) {
    .stage { grid-template-columns: 1fr; grid-template-rows: minmax(0, 1fr) auto; }
    .side.rank-panel { max-height: 36vh; order: 2; }
    .board-wrap { order: 1; }
    .form-grid { grid-template-columns: 1fr; }
    .seat-name { font-size: 14px; }
  }
    .chart-grid { grid-template-columns: 1fr; }
    .tt-focus-grid { grid-template-columns: 1fr 1fr; gap: 8px; }
    .tt-focus-card { padding: 10px 12px; min-height: 88px; }
    .tt-period { font-size: 13px; }
    .tt-focus-item,
    .tt-focus-body .tt-class { font-size: 22px; min-width: 56px; min-height: 36px; }
    .tt-focus-action .tool { font-size: 13px; min-height: 36px; padding: 5px 10px; }
    .lab-board { grid-template-columns: repeat(2, minmax(0, 1fr)); grid-template-rows: repeat(3, minmax(140px, 1fr)); }
  }
  .setup-card ol { margin: 8px 0 0; padding-left: 20px; line-height: 1.7; color: var(--muted); }

  body.need-login #app,
  body.need-login #teacherView {
    visibility: hidden;
    pointer-events: none;
  }

  .login-gate {
    position: fixed;
    inset: 0;
    z-index: 80;
    display: grid;
    place-items: center;
    padding: 20px;
    background: var(--paper);
  }

  .login-gate[hidden] {
    display: none !important;
  }

  .login-card {
    width: min(440px, 100%);
    padding: 28px 24px;
    background: #fff;
    border: 1px solid var(--line);
    border-radius: 20px;
    box-shadow: var(--shadow);
    display: grid;
    gap: 12px;
    justify-items: center;
    text-align: center;
  }

  .login-card h1 {
    margin: 0;
    font-size: 22px;
  }

  .login-card .brand-mark {
    width: 48px;
    height: 48px;
    border-radius: 14px;
    display: grid;
    place-items: center;
    background: var(--primary);
    color: #fff;
    font-weight: 700;
  }

  .login-card .hint,
  .login-note {
    margin: 0;
    line-height: 1.6;
  }

  .google-setup {
    width: 100%;
    text-align: left;
    display: grid;
    gap: 10px;
    padding: 12px;
    background: #f7f4ee;
    border-radius: 14px;
  }

  .google-setup ol {
    margin: 0;
    padding-left: 20px;
    color: var(--muted);
    line-height: 1.7;
    font-size: 13px;
  }

  .google-setup code {
    font-size: 12px;
    word-break: break-all;
  }

  .google-setup label,
  .google-setup input {
    width: 100%;
  }

  .google-setup input {
    margin-top: 6px;
    padding: 8px 10px;
    border: 1px solid var(--line);
    border-radius: 10px;
    font-size: 13px;
  }

  #googleSignInBtn {
    min-height: 44px;
    display: grid;
    place-items: center;
  }

  .account-chip {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 13px;
    color: var(--muted);
  }

  .cloud-relogin {
    margin-top: 12px;
    padding: 12px;
    border: 1px dashed var(--accent);
    border-radius: 12px;
    background: #fff7ea;
    display: grid;
    gap: 10px;
    justify-items: start;
  }

  #googleSignInBtnSettings,
  #googleSignInBtn {
    min-height: 44px;
    position: relative;
    z-index: 2;
  }

  #googleSignInBtnSettings iframe,
  #googleSignInBtn iframe {
    pointer-events: auto !important;
  }

  .account-chip [data-account-label] {
    max-width: 220px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  body.role-viewer [data-app-view="teacher"],
  body.role-viewer #btnDatabase,
  body.role-viewer #app > .toolbar,
  body.role-viewer #labBar,
  body.role-viewer #groupBar,
  body.role-viewer #btnGroup,
  body.role-viewer #btnTimer,
  body.role-viewer #timerPanel .timer-controls,
  body.role-viewer #rankEmpty {
    display: none !important;
  }

  body.role-viewer .board .seat-card {
    cursor: default;
  }

  .hw-panel .hw-create {
    margin-top: 8px;
  }

  .hw-actions {
    margin: 8px 0 12px;
  }

  .hw-url {
    font-size: 13px;
    word-break: break-all;
    color: var(--muted);
  }

  .hw-table-wrap {
    max-height: 420px;
    overflow: auto;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: #fff;
  }

  .hw-table-wrap tr.is-picked {
    background: #eef6f2;
  }

  .hw-student-actions {
    background: #fff;
    border: 1px solid var(--line);
    border-radius: 14px;
    padding: 12px;
  }

  .paste-box textarea {
    width: 100%;
    font-family: Consolas, "Noto Sans TC", monospace;
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 8px;
    background: #fff;
  }