/* 習字 APP 樣式表
 * 遵循規範：
 * - 無米白／奶油色背景（使用清爽白與淡冷灰）
 * - 無斜體強調、無 01/02/03 編號區塊、無等寬字體標籤
 * - 按鈕小圓角（<= 8px）、無膠囊形按鈕
 * - 觸控目標 >= 44px，適合小朋友在平板與桌機使用
 * - 寫字格為主角，佔可視高度至少 60%
 */

@font-face {
  font-family: "TW-Kai-Subset";
  src: url("../fonts/TW-Kai-subset.woff2") format("woff2");
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}

:root {
  --font-kai: "TW-Kai-Subset", "TW-Kai-Full", "DFKai-SB", "BiauKai", "標楷體", serif;
  --bg-page: #edf1f7;
  --bg-surface: #ffffff;
  --bg-subtle: #f4f7fb;
  --border-color: #cbd5e1;
  --border-strong: #94a3b8;
  --text-main: #0f172a;
  --text-muted: #475569;
  --accent: #1d4ed8;
  --accent-hover: #1e40af;
  --accent-soft: #dbeafe;
  --grid-red: #d9483b;
  --main-accent: #9a3412;
  --main-soft: #fde8e1;
  --main-border: #ea580c;
  --emerald: #047857;
  --emerald-soft: #d1fae5;
  --danger: #b91c1c;
  --danger-soft: #fee2e2;
  --radius: 6px;
  --radius-sm: 4px;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  background-color: var(--bg-page);
  color: var(--text-main);
  font-family: var(--font-kai);
  font-size: 17px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.hidden {
  display: none !important;
}

.app-shell {
  max-width: 1320px;
  margin: 0 auto;
  padding: 6px 14px 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

/* Top Bar */
.top-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  background: var(--bg-surface);
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  padding: 6px 10px;
}

.brand-title {
  margin: 0;
  font-size: 1.45rem;
  font-weight: 700;
  color: var(--text-main);
  white-space: nowrap;
}

.input-form {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1 1 280px;
  min-width: 0;
}

.char-input {
  flex: 1 1 auto;
  min-width: 0;
  min-height: 44px;
  padding: 6px 12px;
  font-family: var(--font-kai);
  font-size: 1.15rem;
  color: var(--text-main);
  background: var(--bg-subtle);
  border: 2px solid var(--border-color);
  border-radius: var(--radius);
}

.char-input:focus {
  outline: none;
  border-color: var(--accent);
  background: var(--bg-surface);
}

/* Buttons: small border-radius <= 8px, >= 44px touch target, NO pill shape */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  min-width: 44px;
  padding: 8px 14px;
  font-family: var(--font-kai);
  font-size: 1.05rem;
  font-weight: 600;
  color: var(--text-main);
  background: var(--bg-surface);
  border: 1.5px solid var(--border-strong);
  border-radius: var(--radius);
  cursor: pointer;
  white-space: nowrap;
  user-select: none;
}

.btn:hover:not(:disabled) {
  background: var(--bg-subtle);
  border-color: var(--accent);
}

.btn:focus-visible {
  outline: 3px solid var(--accent-soft);
  border-color: var(--accent);
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #ffffff;
}

.btn-primary:hover:not(:disabled) {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

.btn-ghost {
  background: var(--bg-subtle);
  border-color: var(--border-color);
}

.btn-toggle {
  flex: 1 1 0;
  padding: 8px 10px;
}

.btn-toggle.active,
.btn-toggle.active:hover:not(:disabled) {
  background: var(--accent);
  border-color: var(--accent);
  color: #ffffff;
}

/* Character Cards Strip: single row horizontal scroll so long inputs never push writing grid off first screen */
.char-strip-section {
  background: var(--bg-surface);
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  padding: 5px 10px;
  min-width: 0;
  max-width: 100%;
}

.char-cards {
  display: flex;
  flex-wrap: nowrap;
  overflow-x: auto;
  overflow-y: hidden;
  gap: 6px;
  align-items: center;
  padding-bottom: 2px;
  scrollbar-width: thin;
}

.char-card {
  position: relative;
  flex: 0 0 auto;
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-width: 48px;
  min-height: 46px;
  padding: 2px 8px;
  font-family: var(--font-kai);
  background: var(--bg-subtle);
  border: 2px solid var(--border-color);
  border-radius: var(--radius);
  cursor: pointer;
  color: var(--text-main);
  transition: border-color 0.12s ease, background-color 0.12s ease;
}

.char-card:hover {
  border-color: var(--accent);
}

.char-card.active {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
}

.char-card-glyph {
  font-size: 1.5rem;
  line-height: 1.1;
  font-weight: 700;
}

.char-card-missing-tag {
  font-size: 0.7rem;
  line-height: 1.05;
  color: var(--danger);
  background: var(--danger-soft);
  padding: 1px 4px;
  border-radius: var(--radius-sm);
  margin-top: 1px;
}

.char-cards-empty {
  margin: 4px 2px;
  color: var(--text-muted);
  font-size: 1rem;
}

/* Main Workspace: Writing Grid Hero + Side Panel */
.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1.35fr) minmax(300px, 0.9fr);
  gap: 12px;
  align-items: start;
}

.stage-column {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  background: var(--bg-surface);
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  padding: 8px 12px;
}

.stage-wrapper {
  position: relative;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

/* Writing grid takes >= 55% of viewport height while keeping controls visible without scrolling */
.stage-canvas-box {
  position: relative;
  width: min(100%, 58vh);
  height: min(58vh, 100%);
  max-width: 600px;
  max-height: 600px;
  aspect-ratio: 1 / 1;
}

.writing-svg {
  display: block;
  width: 100%;
  height: 100%;
  background: #ffffff;
  border-radius: var(--radius-sm);
}

.writing-svg.has-outside-hint {
  overflow: visible;
}

/* In-grid / outside-grid compact dynamic stroke hint callout */
.stage-hint-box {
  position: absolute;
  z-index: 6;
  pointer-events: none;
  width: max-content;
  max-width: min(176px, 38%);
  padding: 5px 7px;
  background: rgba(255, 255, 255, 0.96);
  border: 2px solid var(--accent);
  border-radius: var(--radius);
  box-shadow: 0 3px 10px rgba(15, 23, 42, 0.16);
  color: var(--text-main);
  font-size: 0.78rem;
  line-height: 1.32;
  overflow-wrap: anywhere;
}

.stage-hint-box.is-outside {
  max-width: min(260px, 92%);
  z-index: 8;
}

.stage-hint-box.is-key-stroke {
  border-color: var(--main-border);
  background: rgba(255, 255, 255, 0.98);
}

.stage-hint-head {
  display: flex;
  align-items: center;
  gap: 4px;
  font-weight: 700;
  font-size: 0.78rem;
  color: var(--accent);
  margin-bottom: 2px;
}

.stage-hint-box.is-key-stroke .stage-hint-head {
  color: var(--main-accent);
}

.stage-hint-key-badge {
  display: inline-block;
  flex-shrink: 0;
  white-space: nowrap;
  padding: 0 5px;
  font-size: 0.72rem;
  font-weight: 700;
  background: var(--main-soft);
  color: var(--main-accent);
  border: 1px solid var(--main-border);
  border-radius: var(--radius-sm);
}

.stage-hint-line {
  margin: 0;
}

.stage-hint-line + .stage-hint-line {
  margin-top: 2px;
}

.stage-hint-rel {
  color: var(--accent-hover);
  font-weight: 600;
}

.stage-hint-key {
  margin-top: 3px;
  padding-top: 3px;
  border-top: 1px solid var(--border-color);
  color: var(--main-accent);
  font-weight: 600;
}

/* Directional pointer triangle (小尖角) on hint box edge facing the stroke */
.stage-hint-box::after {
  content: "";
  position: absolute;
  width: 10px;
  height: 10px;
  background: #ffffff;
  border-right: 2px solid var(--accent);
  border-bottom: 2px solid var(--accent);
  transform: rotate(45deg);
}

.stage-hint-box.is-key-stroke::after {
  border-right-color: var(--main-border);
  border-bottom-color: var(--main-border);
}

.stage-hint-box[data-tail="bottom"]::after {
  bottom: -6px;
  left: var(--tail-offset, 50%);
  margin-left: -5px;
  transform: rotate(45deg);
}

.stage-hint-box[data-tail="top"]::after {
  top: -6px;
  left: var(--tail-offset, 50%);
  margin-left: -5px;
  transform: rotate(-135deg);
}

.stage-hint-box[data-tail="left"]::after {
  left: -6px;
  top: var(--tail-offset, 50%);
  margin-top: -5px;
  transform: rotate(135deg);
}

.stage-hint-box[data-tail="right"]::after {
  right: -6px;
  top: var(--tail-offset, 50%);
  margin-top: -5px;
  transform: rotate(-45deg);
}

.fallback-glyph {
  font-family: var(--font-kai);
  user-select: none;
}

.fallback-banner {
  width: min(100%, 58vh, 600px);
  margin-bottom: 6px;
  padding: 5px 10px;
  background: var(--danger-soft);
  color: var(--danger);
  border: 1px solid #fca5a5;
  border-radius: var(--radius);
  text-align: center;
  font-weight: 700;
  font-size: 1rem;
}

/* Stroke Status Bar */
.stroke-status-bar {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
  padding: 2px 12px;
  min-height: 44px;
  background: var(--bg-subtle);
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  font-size: 1.08rem;
}

.status-main {
  display: flex;
  align-items: center;
  gap: 12px;
}

.auto-next-label {
  padding: 2px 4px;
  font-size: 0.96rem;
}

.status-progress {
  font-weight: 700;
  color: var(--text-main);
}

.status-stroke-name {
  padding: 2px 10px;
  background: var(--accent-soft);
  color: var(--accent);
  border-radius: var(--radius-sm);
  font-weight: 700;
}

/* Playback Controls */
.playback-controls {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

.playback-controls .btn {
  padding: 6px 12px;
  font-size: 1rem;
}

.control-divider {
  width: 1px;
  height: 28px;
  background: var(--border-color);
  margin: 0 2px;
}

.checkbox-label {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 44px;
  padding: 4px 10px;
  cursor: pointer;
  font-size: 1rem;
  user-select: none;
  border-radius: var(--radius);
}

.checkbox-label input[type="checkbox"] {
  width: 20px;
  height: 20px;
  accent-color: var(--accent);
  cursor: pointer;
}

/* Side Column */
.side-column {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.panel {
  background: var(--bg-surface);
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  padding: 12px 14px;
}

.settings-panel {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.setting-row {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.setting-row-inline {
  flex-direction: row;
  align-items: center;
}

.ghost-toggle-label {
  padding: 2px 4px;
  min-height: 44px;
  font-size: 0.98rem;
}

.setting-row-sub {
  padding: 6px 8px;
  background: var(--bg-subtle);
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
}

.setting-label-sub {
  font-size: 0.92rem;
  color: var(--text-muted);
}

.btn-compact {
  padding: 6px 8px;
  font-size: 0.95rem;
}

.setting-label {
  font-size: 1rem;
  font-weight: 700;
  color: var(--text-main);
}

.btn-group {
  display: flex;
  gap: 6px;
  width: 100%;
}

.btn-group-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 6px;
}

.slider-wrap {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 44px;
}

.slider-wrap input[type="range"] {
  flex: 1 1 auto;
  height: 8px;
  accent-color: var(--accent);
  cursor: pointer;
}

.slider-bound {
  font-size: 0.95rem;
  color: var(--text-muted);
}

/* Tips Panel */
.tips-panel {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.tips-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  border-bottom: 1px solid var(--border-color);
  padding-bottom: 8px;
}

.tips-title {
  margin: 0;
  font-size: 1.2rem;
  font-weight: 700;
}

.tips-mode-badge {
  padding: 2px 8px;
  font-size: 0.9rem;
  font-weight: 700;
  background: var(--bg-subtle);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  color: var(--text-muted);
}

.tips-body {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.tips-summary-banner {
  padding: 8px 10px;
  background: var(--bg-subtle);
  border-left: 4px solid var(--accent);
  border-radius: var(--radius-sm);
  font-size: 1rem;
}

.tips-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.tip-card {
  padding: 10px 12px;
  background: var(--bg-subtle);
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.tip-card-main {
  background: var(--main-soft);
  border-color: var(--main-border);
}

.tip-card-dynamic {
  background: #eff6ff;
  border: 2px solid var(--accent);
}

.tip-tag {
  font-size: 0.92rem;
  font-weight: 700;
  color: var(--accent);
}

.tip-card-main .tip-tag {
  color: var(--main-accent);
}

.tip-text {
  margin: 0;
  font-size: 1.05rem;
  color: var(--text-main);
}

.tip-hints-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 4px;
}

.tip-hint-start {
  padding: 2px 8px;
  background: var(--emerald-soft);
  color: var(--emerald);
  border-radius: var(--radius-sm);
  font-size: 0.92rem;
  font-weight: 700;
}

.tip-hint-end {
  padding: 2px 8px;
  background: var(--danger-soft);
  color: var(--danger);
  border-radius: var(--radius-sm);
  font-size: 0.92rem;
  font-weight: 700;
}

.tip-source {
  font-size: 0.85rem;
  color: var(--text-muted);
}

.tips-off-note {
  margin: 4px 0;
  color: var(--text-muted);
  font-size: 1rem;
}

/* Sources Dialog */
.sources-dialog {
  width: min(92vw, 600px);
  padding: 0;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--bg-surface);
  color: var(--text-main);
}

.sources-dialog::backdrop {
  background: rgba(15, 23, 42, 0.45);
}

.dialog-inner {
  padding: 16px 18px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  border-bottom: 1px solid var(--border-color);
  padding-bottom: 10px;
}

.dialog-header h2 {
  margin: 0;
  font-size: 1.25rem;
}

.sources-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.source-item {
  padding: 10px 12px;
  background: var(--bg-subtle);
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
}

.source-item h3 {
  margin: 0 0 4px;
  font-size: 1.08rem;
  color: var(--accent);
}

.source-item p {
  margin: 0;
  font-size: 0.98rem;
  color: var(--text-main);
}

.dialog-footer {
  border-top: 1px solid var(--border-color);
  padding-top: 10px;
}

.license-note {
  margin: 0;
  font-size: 0.9rem;
  color: var(--text-muted);
}

/* Tablet Landscape (901px - 1024px, e.g. iPad landscape 1024x768) */
@media (max-width: 1024px) {
  .workspace {
    grid-template-columns: minmax(0, 1.3fr) minmax(270px, 0.85fr);
    gap: 10px;
  }

  .stage-canvas-box {
    width: min(100%, 57vh);
    height: min(57vh, 100%);
  }

  .playback-controls .btn {
    padding: 6px 9px;
    font-size: 0.95rem;
  }

  .stroke-status-bar {
    padding: 2px 10px;
    font-size: 1rem;
  }
}

/* Tablet Portrait & Narrow Screens (<= 900px, e.g. iPad portrait 768x1024): single column, stage on top */
@media (max-width: 900px) {
  .workspace {
    grid-template-columns: minmax(0, 1fr);
    gap: 10px;
  }

  .stage-column {
    padding: 10px 12px;
  }

  .stage-canvas-box {
    width: min(100%, 66vh);
    height: auto;
    max-width: 680px;
    max-height: 680px;
  }

  .fallback-banner {
    width: min(100%, 66vh, 680px);
  }
}

/* Mobile Portrait (<= 640px, down to 375px with ZERO horizontal scroll) */
@media (max-width: 640px) {
  .app-shell {
    padding: 8px 10px 16px;
    gap: 8px;
  }

  .top-bar {
    padding: 8px 10px;
  }

  .brand-title {
    font-size: 1.25rem;
  }

  .input-form {
    flex: 1 1 100%;
    order: 3;
  }

  .top-bar #btn-sources {
    margin-left: auto;
    padding: 8px 10px;
    font-size: 0.95rem;
  }

  .stage-column {
    padding: 10px;
  }

  .stage-canvas-box {
    width: min(100%, 88vw, 60vh);
    height: auto;
    max-width: 100%;
  }

  .stage-hint-box {
    max-width: min(130px, 38%);
    padding: 4px 6px;
    font-size: 0.7rem;
    line-height: 1.28;
  }

  .stage-hint-box.is-outside {
    max-width: min(240px, 94%);
  }

  .stage-hint-head {
    font-size: 0.7rem;
  }

  .stroke-status-bar {
    justify-content: center;
    padding: 6px 10px;
  }

  .control-divider {
    display: none;
  }

  .playback-controls .btn {
    flex: 1 1 calc(33% - 8px);
    padding: 8px 6px;
    font-size: 0.98rem;
  }

  .btn-group-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}
