*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Noto Sans JP", sans-serif;
  font-size: 16px;
  line-height: 1.6;
  color: #333;
  background: #f0f4f8;
}

/* ==================== ヘッダー ==================== */
header {
  background: linear-gradient(135deg, #1a3a5c 0%, #2c7be5 100%);
  color: white;
  padding: 1rem;
  text-align: center;
}

.header-inner {
  max-width: 720px;
  margin: 0 auto;
  position: relative;
}

.back-link {
  display: inline-block;
  color: rgba(255, 255, 255, 0.85);
  text-decoration: none;
  font-size: 0.875rem;
  padding: 0.375rem 0.75rem;
  border-radius: 4px;
  min-height: 44px;
  line-height: 44px;
  transition: background 0.2s;
}

.back-link:hover {
  background: rgba(255, 255, 255, 0.15);
  color: white;
}

header h1 {
  font-size: 1.35rem;
  margin-top: 0.25rem;
}

.subtitle {
  font-size: 0.85rem;
  opacity: 0.88;
  margin-top: 0.2rem;
}

/* ==================== メインレイアウト ==================== */
main {
  max-width: 720px;
  margin: 0 auto;
  padding: 1rem;
}

/* ==================== 広告ユニット ==================== */
.ad-unit {
  margin: 1.25rem 0;
}

/* ==================== ツールセクション ==================== */
.tool-section {
  background: white;
  border-radius: 12px;
  padding: 1.25rem;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

/* ==================== コントロール行 ==================== */
.controls-row {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  margin-bottom: 1.25rem;
}

/* モードタブ */
.mode-tabs {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.mode-tab {
  flex: 1;
  min-width: 80px;
  padding: 0.6rem 0.75rem;
  min-height: 44px;
  font-size: 0.95rem;
  font-weight: 600;
  border: 2px solid #cdd5df;
  border-radius: 8px;
  background: white;
  color: #555;
  cursor: pointer;
  transition: all 0.2s;
}

.mode-tab:hover {
  border-color: #2c7be5;
  color: #2c7be5;
}

.mode-tab.active {
  background: #2c7be5;
  border-color: #2c7be5;
  color: white;
}

/* テキスト選択 */
.text-select-wrap {
  width: 100%;
}

#text-category {
  width: 100%;
  padding: 0.6rem 0.75rem;
  font-size: 0.95rem;
  border: 2px solid #cdd5df;
  border-radius: 8px;
  background: white;
  min-height: 44px;
  cursor: pointer;
  appearance: auto;
}

#text-category:focus {
  outline: 3px solid #2c7be5;
  outline-offset: 2px;
}

/* スクリーンリーダー専用 */
.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;
}

/* ==================== 統計バー ==================== */
.stats-bar {
  display: flex;
  justify-content: space-around;
  background: #f0f4f8;
  border-radius: 8px;
  padding: 0.75rem 0.5rem;
  margin-bottom: 0.75rem;
}

.stat {
  text-align: center;
}

.stat-label {
  display: block;
  font-size: 0.7rem;
  color: #888;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.stat-value {
  display: block;
  font-size: 1.4rem;
  font-weight: 700;
  color: #1a3a5c;
  font-variant-numeric: tabular-nums;
}

/* ==================== 進捗バー ==================== */
.progress-bar-wrap {
  height: 6px;
  background: #e2e8f0;
  border-radius: 3px;
  margin-bottom: 1rem;
  overflow: hidden;
}

.progress-bar {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, #2c7be5, #38b2ac);
  border-radius: 3px;
  transition: width 0.1s ease;
}

/* ==================== タイピング表示エリア ==================== */
.typing-display {
  background: #f8fafc;
  border: 2px solid #e2e8f0;
  border-radius: 8px;
  padding: 1rem;
  margin-bottom: 0.75rem;
  min-height: 80px;
}

.display-text {
  font-size: 1.25rem;
  line-height: 1.8;
  letter-spacing: 0.03em;
  word-break: break-word;
  font-family: "Courier New", "BIZ UDGothic", monospace;
}

/* 文字のハイライト */
.char {
  display: inline;
  position: relative;
}

.char.correct {
  color: #22863a;
}

.char.incorrect {
  color: #cb2431;
  background: #ffeef0;
  border-radius: 2px;
}

.char.current {
  background: #dbeafe;
  border-bottom: 3px solid #2c7be5;
  border-radius: 2px 2px 0 0;
  animation: cursor-blink 1s step-end infinite;
}

.char.pending {
  color: #aaa;
}

@keyframes cursor-blink {
  0%, 100% { background: #dbeafe; }
  50% { background: #bfdbfe; }
}

/* ローマ字ヒント */
.romaji-hint {
  font-size: 0.8rem;
  color: #2c7be5;
  margin-top: 0.5rem;
  min-height: 1.2em;
  font-family: monospace;
}

/* ==================== 入力フィールド ==================== */
.input-wrap {
  margin-bottom: 0.75rem;
}

.typing-input {
  width: 100%;
  padding: 0.75rem 1rem;
  font-size: 1.05rem;
  border: 2px solid #cdd5df;
  border-radius: 8px;
  min-height: 48px;
  transition: border-color 0.2s, box-shadow 0.2s;
  font-family: inherit;
}

.typing-input:focus {
  outline: none;
  border-color: #2c7be5;
  box-shadow: 0 0 0 3px rgba(44, 123, 229, 0.2);
}

.typing-input:disabled {
  background: #f0f4f8;
  color: #aaa;
  cursor: not-allowed;
}

.typing-input.input-error {
  border-color: #cb2431;
  box-shadow: 0 0 0 3px rgba(203, 36, 49, 0.15);
  animation: shake 0.25s ease;
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-4px); }
  75% { transform: translateX(4px); }
}

/* ==================== ベストスコア ==================== */
.best-score {
  text-align: center;
  font-size: 0.9rem;
  color: #666;
  margin-bottom: 0.75rem;
}

#best-score-display {
  font-weight: 600;
}

/* ==================== アクションボタン ==================== */
.action-buttons {
  display: flex;
  gap: 0.75rem;
}

.btn {
  flex: 1;
  padding: 0.75rem 1rem;
  min-height: 48px;
  font-size: 1rem;
  font-weight: 700;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
}

.btn:active {
  transform: scale(0.97);
}

.btn-primary {
  background: #2c7be5;
  color: white;
}

.btn-primary:hover {
  background: #1a63c5;
}

.btn-secondary {
  background: #e2e8f0;
  color: #444;
}

.btn-secondary:hover {
  background: #cdd5df;
}

/* ==================== モーダル ==================== */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  z-index: 100;
}

.modal[hidden] {
  display: none;
}

.modal-content {
  background: white;
  border-radius: 16px;
  padding: 2rem 1.5rem;
  width: 100%;
  max-width: 400px;
  text-align: center;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
}

.modal-content h2 {
  font-size: 1.4rem;
  color: #1a3a5c;
  margin-bottom: 1rem;
}

.rank-display {
  font-size: 5rem;
  line-height: 1;
  margin: 0.5rem 0;
  font-weight: 900;
  color: #f59e0b;
  text-shadow: 0 2px 8px rgba(245, 158, 11, 0.3);
}

.result-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem;
  margin: 1.25rem 0;
}

.result-stat {
  background: #f0f4f8;
  border-radius: 8px;
  padding: 0.75rem;
}

.result-label {
  display: block;
  font-size: 0.75rem;
  color: #888;
  font-weight: 600;
}

.result-value {
  display: block;
  font-size: 1.5rem;
  font-weight: 700;
  color: #1a3a5c;
}

.new-best {
  display: inline-block;
  background: linear-gradient(135deg, #f59e0b, #ef4444);
  color: white;
  font-weight: 700;
  padding: 0.4rem 1.25rem;
  border-radius: 999px;
  font-size: 0.9rem;
  margin-bottom: 1rem;
  animation: pop 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.new-best[hidden] {
  display: none;
}

@keyframes pop {
  from { transform: scale(0.5); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

.modal-buttons {
  display: flex;
  gap: 0.75rem;
}

/* ==================== 使い方セクション ==================== */
.howto-section {
  margin-top: 1.5rem;
  padding: 1.5rem;
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

.howto-section h2 {
  font-size: 1.05rem;
  color: #1a3a5c;
  margin-bottom: 0.75rem;
  margin-top: 1.5rem;
  padding-bottom: 0.4rem;
  border-bottom: 2px solid #e2e8f0;
}

.howto-section h2:first-child {
  margin-top: 0;
}

.howto-section ol {
  padding-left: 1.5rem;
  color: #555;
  font-size: 0.95rem;
}

.howto-section li {
  margin-bottom: 0.5rem;
}

.howto-section p {
  color: #555;
  font-size: 0.95rem;
  line-height: 1.75;
}

/* ランクテーブル */
.rank-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
  margin-top: 0.75rem;
}

.rank-table th,
.rank-table td {
  padding: 0.5rem 0.75rem;
  text-align: left;
  border-bottom: 1px solid #e2e8f0;
}

.rank-table th {
  background: #f0f4f8;
  font-weight: 700;
  color: #1a3a5c;
}

.rank-table td:first-child {
  font-weight: 700;
  color: #f59e0b;
  font-size: 1.1rem;
}

/* ==================== フッター ==================== */
footer {
  text-align: center;
  padding: 2rem 1rem;
  font-size: 0.85rem;
  color: #888;
}

footer a {
  color: #666;
  text-decoration: underline;
}

footer a:hover {
  color: #333;
}

/* ==================== レスポンシブ ==================== */
@media (min-width: 640px) {
  header h1 {
    font-size: 1.75rem;
  }

  main {
    padding: 1.5rem 2rem;
  }

  .controls-row {
    flex-direction: row;
    align-items: center;
  }

  .mode-tabs {
    flex-shrink: 0;
  }

  .text-select-wrap {
    flex: 1;
  }

  .display-text {
    font-size: 1.4rem;
  }

  .typing-input {
    font-size: 1.1rem;
  }

  .stat-value {
    font-size: 1.6rem;
  }
}

@media (min-width: 480px) {
  .result-stats {
    grid-template-columns: repeat(4, 1fr);
  }
}
