/* ========================================
   ひらがな→ローマ字 手書き練習
   Teachers フォント + レスポンシブ
   ======================================== */

@font-face {
  font-family: "TeachersWeb";
  src: url("./fonts/Teachers-Regular.ttf") format("truetype");
  font-style: normal;
  font-weight: 400;
  font-display: swap;
}

@font-face {
  font-family: "TeachersWeb";
  src: url("./fonts/Teachers-Medium.ttf") format("truetype");
  font-style: normal;
  font-weight: 500;
  font-display: swap;
}

@font-face {
  font-family: "TeachersWeb";
  src: url("./fonts/Teachers-SemiBold.ttf") format("truetype");
  font-style: normal;
  font-weight: 600;
  font-display: swap;
}

:root {
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --success: #16a34a;
  --warning: #ca8a04;
  --danger: #dc2626;
  --surface: #f8fafc;
  --surface-alt: #f1f5f9;
  --text: #1e293b;
  --text-muted: #64748b;
  --radius: 12px;
  --shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
  --font-sans: "TeachersWeb", system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
  --fs-xxl: 2.2rem;
  --fs-xl: 1.5rem;
  --fs-lg: 1.25rem;
  --fs-md: 1rem;
  --fs-sm: 0.875rem;
  --fs-xs: 0.75rem;
  --vh: 1vh;
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  font-family: var(--font-sans);
  font-size: var(--fs-md);
  line-height: 1.5;
  color: var(--text);
  background: var(--surface);
  min-height: 100vh;
  -webkit-tap-highlight-color: transparent;
}

#app {
  min-height: 100vh;
}

/* 各画面の表示切り替え */
.view {
  display: none;
  flex-direction: column;
  min-height: 100vh;
}

.view.active {
  display: flex;
}

/* ヘッダー */
.header {
  padding: 1rem 1.25rem;
  background: white;
  box-shadow: var(--shadow);
}

.header h1, .header h2 {
  margin: 0;
  font-size: var(--fs-xl);
}

.header.compact {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.header.compact h2 {
  font-size: var(--fs-lg);
}

.subtitle {
  margin: 0.25rem 0 0;
  font-size: var(--fs-sm);
  color: var(--text-muted);
}

.back-btn {
  padding: 0.5rem 0.75rem;
  font-size: 1.25rem;
  border: none;
  background: var(--surface-alt);
  border-radius: var(--radius);
  cursor: pointer;
}

.back-btn:hover {
  background: #e2e8f0;
}

/* ホーム画面 */
#home-view {
  padding: 2rem 1rem;
  align-items: center;
  justify-content: flex-start;
}

.home-nav {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  max-width: 320px;
  width: 100%;
  margin-top: 2rem;
}

.home-btn {
  padding: 1.25rem 1.5rem;
  font-size: var(--fs-lg);
  font-weight: 600;
  border: 2px solid var(--primary);
  background: white;
  color: var(--primary);
  border-radius: var(--radius);
  cursor: pointer;
  transition: background 0.2s, color 0.2s;
}

.home-btn:hover {
  background: var(--primary);
  color: white;
}

.home-btn .btn-desc {
  display: block;
  font-size: var(--fs-xs);
  font-weight: 400;
  color: var(--text-muted);
  margin-top: 0.25rem;
}

/* 練習画面：2カラム・画面全体スクロール不要 */
.practice-view-layout {
  height: calc(var(--vh) * 100);
  overflow: hidden;
}

.practice-view-layout .practice-content {
  flex: 1;
  display: flex;
  min-height: 0;
  overflow: hidden;
}

.practice-left {
  width: clamp(320px, 26vw, 360px);
  flex-shrink: 0;
  padding: 0.75rem;
  overflow-y: auto;
  border-right: 1px solid #e2e8f0;
  background: #fff;
}

.practice-right {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  padding: 0.75rem;
  overflow: hidden;
}

.practice-right .problem-area {
  flex-shrink: 0;
}

.practice-right .canvas-container {
  flex: 1;
  min-height: 120px;
  margin: 0.5rem 0;
  display: flex;
  align-items: center;
  justify-content: center;
  max-width: 100%;
}

.practice-right .canvas-container canvas {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}

.practice-right .practice-buttons,
.practice-right .verdict-display,
.practice-right .error-message {
  flex-shrink: 0;
}

.practice-controls {
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  gap: 0.25rem;
  padding-bottom: 0.25rem;
}

:root[data-orientation="portrait"] .practice-view-layout .practice-content {
  flex-direction: column;
}

:root[data-orientation="portrait"] .practice-view-layout .practice-left {
  width: 100%;
  max-height: 40vh;
  border-right: none;
  border-bottom: 1px solid #e2e8f0;
}

:root[data-orientation="portrait"] .practice-view-layout .practice-right .canvas-container {
  min-height: 200px;
}

.hidden {
  display: none !important;
}

.muted {
  color: var(--text-muted);
  font-size: 0.875rem;
}

.loading {
  color: var(--text-muted);
  font-size: 0.875rem;
}

.practice-history-toggle {
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  padding: 0.35rem 0.75rem;
  font-size: 0.75rem;
  border: 1px solid #cbd5e1;
  background: rgba(255, 255, 255, 0.96);
  border-radius: 999px;
  cursor: pointer;
  z-index: 10;
}

.practice-history-preview {
  position: absolute;
  top: 2.5rem;
  right: 0.5rem;
  width: min(40%, 260px);
  max-height: 70%;
  padding: 0.5rem;
  background: var(--surface-alt);
  border-radius: 8px;
  box-shadow: var(--shadow);
  overflow-y: auto;
  z-index: 9;
}

.practice-history-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.practice-history-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0.25rem;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  cursor: pointer;
  background: white;
}

.practice-history-item:hover {
  background: #f8fafc;
}

.practice-history-thumb {
  width: 56px;
  height: 42px;
  border-radius: 4px;
  display: block;
}

.practice-history-label {
  font-size: 0.65rem;
  color: var(--text-muted);
  margin-top: 0.25rem;
  max-width: 56px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.debug-toggle {
  margin-bottom: 0.5rem;
}

.debug-toggle .checkbox-label {
  font-size: 0.75rem;
}

.grading-debug-panel {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-bottom: 0.5rem;
  white-space: pre-line;
}

.problem-area {
  display: flex;
  justify-content: center;
  align-items: baseline;
  gap: 1rem;
  padding: 1rem;
  background: white;
  border-radius: var(--radius);
  margin-bottom: 1rem;
}

.current-kana {
  font-size: var(--fs-xxl);
  font-weight: bold;
}

.current-romaji {
  font-size: var(--fs-lg);
}

.current-romaji.hidden {
  visibility: hidden;
}

.char-select-area {
  margin-bottom: 1rem;
}

.char-select-area label,
.settings-row label {
  display: block;
  font-size: 0.875rem;
  margin-bottom: 0.25rem;
}

#char-category {
  width: 100%;
  max-width: 240px;
  padding: 0.5rem;
  border-radius: 8px;
  border: 1px solid #cbd5e1;
  font-size: 1rem;
}

.char-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 0.5rem;
  margin-top: 0.5rem;
}

.char-btn {
  position: relative;
  width: 2.75rem;
  height: 2.75rem;
  min-width: 44px;
  min-height: 44px;
  font-size: 1.25rem;
  border: 1px solid #cbd5e1;
  background: white;
  border-radius: 8px;
  cursor: pointer;
  padding: 0;
}

.char-btn:hover {
  background: var(--surface-alt);
}

.char-btn.active {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

/* 達成状況（bestScore に応じて着色） */
.char-btn.achieved80 {
  background: #e8f9ee;
  border-color: #8bd3a6;
}

.char-btn.achieved90::after {
  content: "★";
  position: absolute;
  top: 3px;
  right: 4px;
  font-size: 0.7rem;
  color: #f59e0b;
}

/* 達成済みかつ選択中: 枠線は青、背景にうっすら緑を残す */
.char-btn.active.achieved80 {
  background: linear-gradient(135deg, #e8f9ee, #dbeafe);
  color: #1e293b;
  border-color: var(--primary);
}

.char-btn.active.achieved90::after {
  content: "★";
}

.char-cell-empty {
  width: 2.5rem;
  height: 2.5rem;
}

.settings-row {
  margin-bottom: 1rem;
}

#difficulty {
  width: 100%;
  max-width: 240px;
  padding: 0.5rem;
  border-radius: 8px;
  border: 1px solid #cbd5e1;
  font-size: 1rem;
}

.support-sliders {
  margin-bottom: 1rem;
}

.slider-group {
  margin-bottom: 0.75rem;
}

.slider-group label {
  display: block;
  font-size: 0.875rem;
  margin-bottom: 0.25rem;
}

.slider-group input[type="range"] {
  width: 100%;
  max-width: 280px;
}

.canvas-container {
  position: relative;
  width: 100%;
  max-width: 400px;
  margin: 0 auto 1rem;
  background: #fff;
  border-radius: var(--radius);
  overflow: hidden;
  touch-action: none; /* スクロール誤爆対策 */
}

#draw-canvas,
#feedback-canvas {
  display: block;
  width: 100%;
  height: auto;
  touch-action: none;
}

.template-layer {
  position: absolute;
  top: 0;
  left: 0;
  pointer-events: none;
}

.feedback-overlay {
  position: absolute;
  top: 0;
  left: 0;
  pointer-events: none;
}

.practice-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  margin-bottom: 1rem;
}

.action-btn {
  padding: 0.75rem 1.25rem;
  font-size: 1rem;
  border: 1px solid #cbd5e1;
  background: white;
  border-radius: var(--radius);
  cursor: pointer;
}

.action-btn:hover {
  background: var(--surface-alt);
}

.action-btn.primary {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

.action-btn.primary:hover {
  background: var(--primary-hover);
}

.verdict-display {
  padding: 1rem;
  font-size: 1.25rem;
  font-weight: 600;
  text-align: center;
  border-radius: var(--radius);
  min-height: 4.5rem;
  display: flex;
  align-items: center;
  justify-content: center;
  box-sizing: border-box;
}

.verdict-display.green {
  background: #dcfce7;
  color: var(--success);
}

.verdict-display.yellow {
  background: #fef9c3;
  color: #a16207;
}

.verdict-display.red {
  background: #fee2e2;
  color: var(--danger);
}

/* エラーメッセージ */
.error-message {
  padding: 0.75rem;
  margin-top: 1rem;
  background: #fee2e2;
  color: var(--danger);
  border-radius: 8px;
  font-size: 0.875rem;
}

.error-message:empty {
  display: none;
}

/* テスト画面 */
.test-view-layout {
  height: calc(var(--vh) * 100);
  overflow: hidden;
}

.test-content {
  flex: 1;
  padding: 1rem;
  overflow-y: auto;
}

.test-config h3,
.test-result h3 {
  margin-top: 0;
  margin-bottom: 1rem;
}

.test-config-row {
  margin-bottom: 1rem;
}

.test-config-row label {
  display: block;
  font-size: 0.875rem;
  margin-bottom: 0.25rem;
}

.test-config-row select,
.test-config-row input[type="range"] {
  width: 100%;
  max-width: 280px;
}

.test-select-chars {
  margin-bottom: 1rem;
}

.test-select-chars label {
  display: block;
  font-size: 0.875rem;
  margin-bottom: 0.5rem;
}

.test-char-set {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.test-char-select.selected {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

#test-start-btn {
  margin-top: 1rem;
}

.test-problem-area {
  text-align: center;
  padding: 1rem;
}

.test-current-kana {
  font-size: 2.5rem;
  font-weight: bold;
}

.test-current-romaji {
  font-size: 1.25rem;
  margin-left: 0.5rem;
}

.test-canvas-container {
  position: relative;
  width: 100%;
  max-width: 400px;
  min-height: calc(var(--vh) * 40);
  margin: 0 auto 1rem;
  background: #fff;
  border-radius: var(--radius);
  overflow: hidden;
  touch-action: none;
}

/* iOS Safari などでのテキスト選択・コールアウト抑止 */
canvas,
.canvas-container,
.test-canvas-container,
.replay-canvas-container {
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

.test-canvas-container canvas {
  display: block;
  width: 100%;
  height: auto;
}

.test-run-buttons {
  display: flex;
  gap: 0.75rem;
  margin-bottom: 1rem;
  justify-content: center;
}

.test-wrong-list {
  margin: 1rem 0;
}

.test-wrong-list p {
  margin-bottom: 0.5rem;
}

.test-wrong-list .char-btn {
  margin: 0.25rem;
}

/* 履歴画面 */
.history-content {
  flex: 1;
  padding: 1rem;
  overflow-y: auto;
}

.history-filters {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 1rem;
}

.history-filters select {
  flex: 1;
  min-width: 100px;
  padding: 0.5rem;
  border-radius: 8px;
  border: 1px solid #cbd5e1;
}

.history-actions {
  margin-bottom: 1rem;
}

.danger-btn {
  padding: 0.5rem 1rem;
  background: #fee2e2;
  color: var(--danger);
  border: 1px solid #fecaca;
  border-radius: 8px;
  cursor: pointer;
}

.danger-btn:hover {
  background: #fecaca;
}

.history-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.history-item {
  padding: 1rem;
  background: white;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  cursor: pointer;
}

.history-item:hover {
  background: var(--surface-alt);
}

.history-item-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.history-item-kana {
  font-size: 1.5rem;
  font-weight: bold;
}

.history-item-verdict {
  padding: 0.25rem 0.5rem;
  border-radius: 6px;
  font-size: 0.75rem;
}

.history-item-verdict.green { background: #dcfce7; }
.history-item-verdict.yellow { background: #fef9c3; }
.history-item-verdict.red { background: #fee2e2; }

.history-item-meta {
  font-size: 0.875rem;
  color: var(--text-muted);
  margin-top: 0.25rem;
}

.history-detail {
  margin-top: 1rem;
  padding: 1rem;
  background: white;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.history-detail.hidden {
  display: none !important;
}

.detail-info {
  font-size: 0.875rem;
  margin-bottom: 1rem;
}

.replay-controls {
  margin-bottom: 1rem;
}

.replay-controls label {
  margin-right: 0.5rem;
}

.checkbox-label {
  display: inline-flex;
  align-items: center;
  margin-left: 1rem;
}

.replay-canvas-container {
  position: relative;
  max-width: 400px;
  margin: 0 auto 1rem;
  background: var(--surface-alt);
  border-radius: var(--radius);
}

#replay-canvas {
  display: block;
  width: 100%;
  height: auto;
}

.replay-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

/* タブレット・デスクトップ */
@media (min-width: 600px) {
  .home-nav {
    flex-direction: row;
    flex-wrap: wrap;
    max-width: 540px;
    justify-content: center;
  }

  .home-btn {
    flex: 1;
    min-width: 160px;
  }

  .char-grid {
    max-width: 400px;
  }
}

@media (min-width: 768px) {
  .history-content {
    max-width: 700px;
    margin: 0 auto;
  }
}
