:root {
  --primary: #344b71;
  --on-primary: #ffffff;
  --primary-container: #e1e9f6;
  --on-primary-container: #273d60;
  --secondary-container: #e0ede7;
  --on-secondary-container: #294d40;
  --tertiary-container: #f4e8d7;
  --on-tertiary-container: #654c2f;
  --bg: #f6f7fa;
  --surface: #fcfcfe;
  --surface-low: #f0f2f7;
  --surface-high: #e9edf4;
  --outline: #dde2eb;
  --text: #202c40;
  --text-muted: #677387;
  --error: #ba1a1a;
  --error-bg: rgba(186, 26, 26, 0.1);
  --today-col: rgba(52, 75, 113, 0.045);
  --active-cell: rgba(52, 75, 113, 0.08);
  --active-slot-bg: rgba(52, 75, 113, 0.14);
  --shadow: 0 10px 30px rgba(20, 32, 54, 0.14);
  --radius-sm: 6px;
  --radius-md: 12px;
  --radius-lg: 18px;
}

:root[data-theme="dark"] {
  --primary: #b5c7e8;
  --on-primary: #213551;
  --primary-container: #2b3d59;
  --on-primary-container: #dce7fb;
  --secondary-container: #293f37;
  --on-secondary-container: #cee8da;
  --tertiary-container: #493d2d;
  --on-tertiary-container: #eedcc3;
  --bg: #121820;
  --surface: #171f29;
  --surface-low: #1e2835;
  --surface-high: #263140;
  --outline: #313d4e;
  --text: #e5eaf2;
  --text-muted: #a9b6c8;
  --error: #ffb4ab;
  --error-bg: rgba(255, 180, 171, 0.14);
  --today-col: rgba(181, 199, 232, 0.055);
  --active-cell: rgba(181, 199, 232, 0.1);
  --active-slot-bg: rgba(181, 199, 232, 0.16);
  --shadow: 0 12px 32px rgba(0, 0, 0, 0.45);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Segoe UI", sans-serif;
  font-size: 14px;
  line-height: 1.4;
  overscroll-behavior-y: contain;
}

#app {
  height: 100dvh;
  max-width: 680px;
  margin: 0 auto;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
  overflow: hidden;
  position: relative;
}

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

button {
  cursor: pointer;
  border: none;
  background: transparent;
}

button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

/* Header & Toolbar */
.topbar {
  padding: 4px 12px 0;
  flex-shrink: 0;
  background: var(--bg);
}

.topbar-main {
  min-height: 46px;
  display: flex;
  align-items: center;
  gap: 4px;
}

.app-title {
  font-size: 20px;
  font-weight: 600;
  letter-spacing: -0.01em;
  margin-right: auto;
  display: flex;
  align-items: center;
  gap: 8px;
}

.app-title-tag {
  font-size: 11px;
  font-weight: 500;
  padding: 2px 6px;
  border-radius: 999px;
  background: var(--surface-high);
  color: var(--text-muted);
}

.btn-text {
  padding: 6px 8px;
  border-radius: 10px;
  color: var(--primary);
  font-weight: 500;
  font-size: 14px;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  white-space: nowrap;
  flex-shrink: 0;
}

.btn-text:active {
  background: var(--surface-high);
}

.btn-tonal {
  padding: 7px 13px;
  border-radius: 12px;
  background: var(--secondary-container);
  color: var(--on-secondary-container);
  font-weight: 600;
  font-size: 13px;
}

.btn-primary {
  padding: 10px 16px;
  border-radius: 12px;
  background: var(--primary);
  color: var(--on-primary);
  font-weight: 600;
  font-size: 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

.btn-outline {
  padding: 9px 14px;
  border-radius: 12px;
  border: 1px solid var(--outline);
  background: var(--surface);
  color: var(--text);
  font-weight: 500;
  font-size: 13px;
}

.btn-danger {
  color: var(--error);
}

.btn-full {
  width: 100%;
}

.icon-btn {
  padding: 4px 7px;
  border-radius: 10px;
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--text);
  min-width: 38px;
}

.icon-btn:active {
  background: var(--surface-high);
}

.icon-btn svg {
  width: 20px;
  height: 20px;
  stroke: currentColor;
  fill: none;
  stroke-width: 1.75;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.icon-btn span {
  font-size: 9px;
  line-height: 11px;
  color: var(--text-muted);
}

.badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 17px;
  height: 17px;
  padding: 0 5px;
  border-radius: 999px;
  background: var(--error);
  color: #ffffff;
  font-size: 10px;
  font-weight: 700;
}

:root[data-theme="dark"] .badge {
  color: #201a1a;
}

.weekbar {
  display: flex;
  align-items: center;
  padding: 2px 0 6px;
  gap: 4px;
}

.week-meta {
  flex: 1;
  min-width: 0;
}

.week-title {
  font-size: 21px;
  font-weight: 600;
  line-height: 1.2;
}

.week-sub {
  font-size: 12px;
  color: var(--text-muted);
}

.notice-stack {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding-bottom: 4px;
}

.notice-bar {
  font-size: 12px;
  color: var(--text-muted);
  padding: 4px 2px;
}

.notice-action {
  text-align: left;
  font-size: 12px;
  color: var(--primary);
  padding: 3px 2px;
  font-weight: 500;
}

.ringing-banner {
  background: var(--error-bg);
  color: var(--error);
  border-radius: 12px;
  padding: 8px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 4px;
}

/* One-screen 17-row timetable board */
.board-wrap {
  flex: 1;
  min-height: 0;
  position: relative;
  user-select: none;
  touch-action: pan-y;
}

.board {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.board-header-cell {
  position: absolute;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  font-size: 10px;
  line-height: 1.15;
  color: var(--text-muted);
  border-radius: 7px;
  padding: 0 2px;
  white-space: pre-line;
}

.board-header-cell.is-today {
  background: var(--primary);
  color: var(--on-primary);
  font-weight: 700;
}

.board-today-col {
  position: absolute;
  background: var(--today-col);
  pointer-events: none;
}

.board-active-row {
  position: absolute;
  background: var(--active-cell);
  pointer-events: none;
}

.board-grid-svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.board-slot-label {
  position: absolute;
  left: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  font-size: 9.5px;
  line-height: 1.05;
  color: var(--text-muted);
  pointer-events: none;
}

.board-slot-label.is-active {
  background: var(--active-slot-bg);
  color: var(--primary);
  font-weight: 700;
}

.board-time-label {
  position: absolute;
  text-align: right;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color: var(--text-muted);
  line-height: 1;
  pointer-events: none;
  white-space: nowrap;
}

.board-empty-cell {
  position: absolute;
  cursor: pointer;
}

.board-empty-cell:active {
  background: var(--active-cell);
}

.board-card {
  position: absolute;
  padding: 1px;
  cursor: pointer;
}

.board-card-inner {
  width: 100%;
  height: 100%;
  border-radius: 5px;
  padding: 2px 3px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
}

.board-card-inner.tone-0 {
  background: var(--primary-container);
  color: var(--on-primary-container);
}

.board-card-inner.tone-1 {
  background: var(--secondary-container);
  color: var(--on-secondary-container);
}

.board-card-inner.tone-2 {
  background: #e4e7f5;
  color: #2c3556;
}

:root[data-theme="dark"] .board-card-inner.tone-2 {
  background: #2b334e;
  color: #dce2f9;
}

.board-card-inner.is-event {
  background: var(--tertiary-container);
  color: var(--on-tertiary-container);
  border-left: 2px solid currentColor;
}

.board-card-title {
  font-size: 10px;
  font-weight: 600;
  line-height: 1.12;
  overflow: hidden;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 4;
  word-break: break-all;
}

.board-card-sub {
  font-size: 8.5px;
  line-height: 1.1;
  opacity: 0.82;
  margin-top: 1px;
  overflow: hidden;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  word-break: break-all;
}

.board-card-inner.is-compact {
  padding: 1px 2px;
  justify-content: center;
}

.board-card-inner.is-compact .board-card-title {
  font-size: 8.5px;
  line-height: 1.08;
  -webkit-line-clamp: 2;
}

.board-card-inner.is-compact .board-card-sub {
  font-size: 7.5px;
  line-height: 1.05;
  -webkit-line-clamp: 1;
  white-space: nowrap;
  text-overflow: ellipsis;
  display: block;
}

.board-time-marker {
  position: absolute;
  border: 1.5px solid var(--primary);
  border-radius: 3px;
  pointer-events: none;
}

.board-break-marker {
  position: absolute;
  height: 2px;
  background: var(--primary);
  pointer-events: none;
}

/* Sub-pages */
.subpage {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--bg);
}

.subpage-bar {
  display: flex;
  align-items: center;
  padding: 6px 10px;
  gap: 6px;
  background: var(--surface);
  border-bottom: 1px solid var(--outline);
  flex-shrink: 0;
}

.subpage-title {
  font-size: 17px;
  font-weight: 600;
  flex: 1;
}

.tabs {
  display: flex;
  background: var(--surface);
  border-bottom: 1px solid var(--outline);
  flex-shrink: 0;
}

.tab-btn {
  flex: 1;
  padding: 11px 6px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-muted);
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
}

.tab-btn.is-active {
  color: var(--primary);
  font-weight: 600;
}

.tab-btn.is-active::after {
  content: "";
  position: absolute;
  bottom: 0;
  left: 20%;
  right: 20%;
  height: 2.5px;
  border-radius: 2px;
  background: var(--primary);
}

.scroll-body {
  flex: 1;
  overflow-y: auto;
  padding: 14px 16px 28px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  -webkit-overflow-scrolling: touch;
}

/* Cards & Forms */
.card {
  background: var(--surface);
  border: 1px solid var(--outline);
  border-radius: var(--radius-md);
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.card-soft {
  background: var(--surface-low);
  border-radius: var(--radius-md);
  padding: 13px 14px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.card-active {
  background: var(--primary-container);
  color: var(--on-primary-container);
}

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

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

.field {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.field-label {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
}

.input,
.textarea,
.select {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--outline);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 15px;
  outline: none;
}

.input:focus,
.textarea:focus,
.select:focus {
  border-color: var(--primary);
}

.textarea {
  resize: vertical;
  min-height: 96px;
  line-height: 1.45;
}

.chip {
  padding: 6px 12px;
  border-radius: 999px;
  border: 1px solid var(--outline);
  background: var(--surface);
  font-size: 13px;
  color: var(--text);
}

.chip.is-selected {
  background: var(--primary-container);
  color: var(--on-primary-container);
  border-color: var(--primary);
  font-weight: 600;
}

.helper {
  font-size: 12px;
  color: var(--text-muted);
}

.error-text {
  font-size: 13px;
  color: var(--error);
}

.success-text {
  font-size: 13px;
  color: var(--on-secondary-container);
  background: var(--secondary-container);
  padding: 8px 12px;
  border-radius: 10px;
}

.divider {
  height: 1px;
  background: var(--outline);
  margin: 4px 0;
}

.section-title {
  font-size: 15px;
  font-weight: 600;
  color: var(--primary);
}

.empty-state {
  text-align: center;
  padding: 36px 16px;
  color: var(--text-muted);
  display: flex;
  flex-direction: column;
  gap: 10px;
  align-items: center;
}

/* Deadline check item */
.deadline-row {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--surface-low);
  border-radius: var(--radius-md);
  padding: 10px 12px;
}

.deadline-check {
  width: 22px;
  height: 22px;
  accent-color: var(--primary);
  flex-shrink: 0;
}

.deadline-main {
  flex: 1;
  min-width: 0;
  cursor: pointer;
}

.deadline-done {
  text-decoration: line-through;
  opacity: 0.6;
}

/* Captcha image */
.captcha-box {
  background: #ffffff;
  border: 1px solid var(--outline);
  border-radius: 10px;
  padding: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.captcha-box img {
  max-width: 100%;
  max-height: 76px;
  object-fit: contain;
}

/* Switch toggle */
.switch-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 4px 0;
}

.switch {
  width: 44px;
  height: 26px;
  accent-color: var(--primary);
}

/* Modal & Bottom Sheet */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 22, 34, 0.5);
  z-index: 40;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

@media (min-width: 540px) {
  .modal-backdrop {
    align-items: center;
    padding: 16px;
  }
}

.modal-sheet {
  width: 100%;
  max-width: 520px;
  max-height: 88dvh;
  background: var(--surface);
  border-radius: 20px 20px 0 0;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  padding-bottom: env(safe-area-inset-bottom);
}

@media (min-width: 540px) {
  .modal-sheet {
    border-radius: 20px;
  }
}

.modal-header {
  padding: 16px 18px 10px;
  font-size: 18px;
  font-weight: 600;
  border-bottom: 1px solid var(--outline);
}

.modal-body {
  padding: 14px 18px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.modal-footer {
  padding: 10px 16px 14px;
  border-top: 1px solid var(--outline);
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

/* Toast */
.toast-root {
  position: fixed;
  bottom: calc(20px + env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  z-index: 60;
  pointer-events: none;
}

.toast-pill {
  background: var(--text);
  color: var(--surface);
  padding: 9px 16px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 500;
  box-shadow: var(--shadow);
}

.progress-bar {
  height: 4px;
  width: 100%;
  background: var(--surface-high);
  border-radius: 999px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: var(--primary);
  transition: width 0.25s ease;
}

.raw-pre {
  background: var(--surface-low);
  padding: 10px;
  border-radius: 8px;
  font-size: 12px;
  white-space: pre-wrap;
  word-break: break-all;
}
