:root {
  /* テーマで切り替えない色 — えんじ系・アクセント */
  --enji: #9d2933;
  --enji-light: #c44d5b;
  --enji-bright: #e25864;
  --accent: #ffd166;

  /* ダークテーマの既定値（OS や localStorage で上書きされない場合の値） */
  --bg: #1a1a1a;
  --panel-bg: #262626;
  --text: #fafafa;
  --tile-bg: #3a3a3a;
  --tile-text: #fafafa;
  --tile-func-bg: #4a3a3a;
  --tile-sub: #bbb;
  --topbar-bg: #000;
  --topbar-hint: #999;
  --footer-bg: #000;
  --footer-border: #333;
  --footer-text: #bbb;
  --btn-bg: #1f1f1f;
  --btn-border: #444;
  --btn-hover-bg: #2a2a2a;
  --kbd-bg: #333;
  --kbd-border: #555;
  --row-highlight-bg: #4a3030;
  --col-highlight-bg: #5a2030;
  --dakuten-highlight-bg: #6a4020;
}

/* ライトテーマ — ユーザーが明示的に切り替えた場合 */
[data-theme="light"] {
  --bg: #f5f5f5;
  --panel-bg: #ffffff;
  --text: #1a1a1a;
  --tile-bg: #e2e2e2;
  --tile-text: #1a1a1a;
  --tile-func-bg: #f5dadd;
  --tile-sub: #555;
  --topbar-bg: #ffffff;
  --topbar-hint: #555;
  --footer-bg: #ffffff;
  --footer-border: #d0d0d0;
  --footer-text: #444;
  --btn-bg: #f0f0f0;
  --btn-border: #c0c0c0;
  --btn-hover-bg: #e0e0e0;
  --kbd-bg: #e0e0e0;
  --kbd-border: #b0b0b0;
  --row-highlight-bg: #ffd5dd;
  --col-highlight-bg: #ffb8c4;
  --dakuten-highlight-bg: #ffe5b8;
}

/* OS のライト設定に追従（ユーザーが明示的に指定していない場合のみ） */
@media (prefers-color-scheme: light) {
  :root:not([data-theme]) {
    --bg: #f5f5f5;
    --panel-bg: #ffffff;
    --text: #1a1a1a;
    --tile-bg: #e2e2e2;
    --tile-text: #1a1a1a;
    --tile-func-bg: #f5dadd;
    --tile-sub: #555;
    --topbar-bg: #ffffff;
    --topbar-hint: #555;
    --footer-bg: #ffffff;
    --footer-border: #d0d0d0;
    --footer-text: #444;
    --btn-bg: #f0f0f0;
    --btn-border: #c0c0c0;
    --btn-hover-bg: #e0e0e0;
    --kbd-bg: #e0e0e0;
    --kbd-border: #b0b0b0;
    --row-highlight-bg: #ffd5dd;
    --col-highlight-bg: #ffb8c4;
    --dakuten-highlight-bg: #ffe5b8;
  }
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  /* UD フォント（BIZ UDPGothic / UD デジタル 教科書体）を最優先。低視力・
     ディスレクシア・高齢ユーザー向けに字形が読み取りやすいため。
     未搭載環境では従来の Hiragino / Yu Gothic / Meiryo にフォールバック */
  font-family: 'BIZ UDPGothic', 'UD デジタル 教科書体 N-R',
               'Hiragino Kaku Gothic ProN', 'Yu Gothic UI', 'Meiryo', sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  user-select: none;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 10px 20px;
  background: var(--topbar-bg);
  border-bottom: 3px solid var(--enji);
  flex: 0 0 auto;
}
.title { font-size: 11px; font-weight: bold; color: var(--enji-light); }
.hint { font-size: 11px; color: var(--topbar-hint); white-space: nowrap; margin-left: auto; }
.input-status {
  display: flex;
  gap: 6px;
  align-items: center;
  font-size: 16px;
}
.input-status-item {
  opacity: 0.3;
  filter: grayscale(1);
  transition: opacity 200ms, filter 200ms;
}
.input-status-item.connected {
  opacity: 1;
  filter: grayscale(0);
}
@media (max-width: 700px) {
  .topbar { gap: 12px; padding: 8px 12px; }
  .hint { display: none; }
  .input-status { margin-left: auto; }
}
kbd {
  background: var(--kbd-bg); padding: 2px 6px; border-radius: 3px;
  border: 1px solid var(--kbd-border); font-family: monospace; font-size: 10px;
}

.text-area { padding: 8px 20px; flex: 0 0 auto; }
#composed {
  width: 100%;
  height: clamp(60px, 14vh, 130px);
  font-size: clamp(24px, 7vh, 52px);
  padding: 10px 14px;
  background: #fff; color: #000;
  border: 3px solid var(--enji);
  border-radius: 6px;
  resize: none;
  font-family: inherit;
}

main {
  flex: 1 1 0;
  min-height: 0;
  padding: clamp(4px, 1.5vmin, 12px) clamp(8px, 2vmin, 20px);
  overflow: hidden;
}

.grid {
  display: grid;
  gap: clamp(2px, 0.6vmin, 6px);
  height: 100%;
}

.tile {
  background: var(--tile-bg);
  color: var(--tile-text);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  padding: clamp(2px, 0.6vmin, 10px);
  font-size: clamp(16px, 4.5vmin, 64px);
  border-radius: 6px;
  min-height: 0;
  min-width: 0;
  border: 2px solid transparent;
  transition: background 0.08s, border-color 0.08s;
  text-align: center;
  line-height: 1.1;
  overflow: hidden;
  position: relative; /* tap-flash-overlay の絶対配置基準 */
}
/* タップ・クリック時にタイル内に重ねる押下フィードバック用オーバーレイ。
   タイル本体の transform/filter を触らないので、列ハイライトや
   highlight-boost アニメーションと完全に独立して必ず表示される */
.tap-flash-overlay {
  position: absolute;
  inset: 0;
  background: var(--accent);
  border-radius: inherit;
  pointer-events: none;
  mix-blend-mode: screen;
  animation: tap-flash-overlay 280ms ease-out forwards;
}
@keyframes tap-flash-overlay {
  from { opacity: 0.85; }
  to   { opacity: 0; }
}
/* タップ中はその瞬間だけ走査枠（赤枠／box-shadow／背景色）を素のタイルへ
   戻して、押下フィードバックが赤枠に紛れないようにする。
   .row-highlight 等が !important を使っているため、ここも !important で
   かつ specificity を上げて確実に勝たせる */
.tile.tap-flashing {
  border-color: transparent !important;
  background: var(--tile-bg) !important;
  box-shadow: none !important;
  animation: none !important;
}
.tile.small { font-size: clamp(12px, 2.2vmin, 24px); }
.tile .sub { font-size: clamp(8px, 1.4vmin, 12px); color: var(--tile-sub); margin-top: 4px; }
.tile.empty { background: transparent; pointer-events: none; }
.tile.func { background: var(--tile-func-bg); font-size: clamp(11px, 2vmin, 22px); }
/* 操作不可のセル：走査・クリックの両方からスキップされる。
   存在自体は見せて位置を学習させたいので、半透明にして disable を表現する */
.tile.disabled {
  opacity: 0.35;
  pointer-events: none;
  cursor: not-allowed;
}

/* Scanning highlights — えんじ色の枠 */
/* 行ハイライト（列全体）：赤枠だけだと P/D 型色覚で識別しづらいので、
   背景にも輝度差を付けて色＋明度の二重符号化にする */
.row-highlight {
  border-color: var(--enji-bright) !important;
  box-shadow: inset 0 0 0 4px var(--enji);
  background: var(--row-highlight-bg) !important;
}
.col-highlight {
  border-color: var(--accent) !important;
  background: var(--col-highlight-bg) !important;
  box-shadow: inset 0 0 0 4px var(--enji-bright);
}
/* 列ハイライト中の 1 セルに、色非依存の形状ヒント（▼）を上端に重ねる。
   .tap-flashing 中は隠して押下フィードバックの邪魔をしない */
.col-highlight:not(.tap-flashing)::before {
  content: '';
  position: absolute;
  top: 2px;
  left: 50%;
  transform: translateX(-50%);
  width: 0;
  height: 0;
  border-left: 6px solid transparent;
  border-right: 6px solid transparent;
  border-top: 8px solid var(--accent);
  pointer-events: none;
  transition: opacity 500ms linear;
}
.col-highlight.fading::before { opacity: 0; }
/* フォーカスが移った直後は輝度を盛り上げて、通常のハイライト色へ収束させる */
.row-highlight,
.col-highlight {
  animation: highlight-boost 220ms ease-out;
}
@keyframes highlight-boost {
  from { filter: brightness(1.2); }
  to   { filter: brightness(1); }
}
/* 前庭障害・てんかん配慮：OS 設定でアニメーション抑制を希望するユーザーには
   ハイライトのアニメーション／フェードを無効化する */
@media (prefers-reduced-motion: reduce) {
  .row-highlight,
  .col-highlight,
  .dakuten-highlight {
    animation: none !important;
  }
  .row-highlight.fading,
  .col-highlight.fading {
    transition: none !important;
  }
  .col-highlight:not(.tap-flashing)::before { transition: none !important; }
}
/* 次のセルへ移る直前の 500ms で色を徐々にフェードして移動を予告 */
.row-highlight.fading,
.col-highlight.fading {
  transition: background 500ms linear, border-color 500ms linear, box-shadow 500ms linear;
}
.row-highlight.fading {
  border-color: transparent !important;
  box-shadow: inset 0 0 0 4px transparent;
}
.col-highlight.fading {
  border-color: transparent !important;
  background: var(--tile-bg) !important;
  box-shadow: inset 0 0 0 4px transparent;
}
.dakuten-highlight {
  border-color: var(--accent) !important;
  background: var(--dakuten-highlight-bg) !important;
  animation: pulse 0.6s ease-in-out infinite alternate;
}
@keyframes pulse {
  from { box-shadow: inset 0 0 0 4px var(--accent); }
  to   { box-shadow: inset 0 0 0 8px var(--accent); }
}

.settings-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  padding: 8px 20px;
  background: var(--footer-bg);
  border-top: 1px solid var(--footer-border);
  font-size: 13px;
  color: var(--footer-text);
  flex: 0 0 auto;
  flex-wrap: wrap;
}
.settings-bar strong { color: var(--enji-light); }
.settings-left  { display: flex; align-items: center; gap: 16px; }
.settings-right { display: flex; align-items: center; gap: 16px; }

.speed-btn {
  cursor: pointer;
  padding: 6px 12px;
  border: 1px solid var(--btn-border);
  border-radius: 4px;
  background: var(--btn-bg);
  color: var(--text);
  user-select: none;
  font-family: inherit;
  font-size: inherit;
}
.speed-btn:hover { background: var(--btn-hover-bg); border-color: var(--enji); }
.speed-btn:active { background: var(--enji); border-color: var(--enji-light); }

.overlay {
  position: fixed; inset: 0;
  background: rgba(0, 0, 0, 0.85);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
}
.overlay.hidden { display: none; }
.modal {
  background: var(--panel-bg);
  padding: 30px;
  border: 3px solid var(--enji);
  border-radius: 10px;
  text-align: center;
  max-width: 600px;
  min-width: 360px;
}
.modal h2 { color: var(--enji-light); margin-bottom: 14px; font-size: 24px; }
.modal p { font-size: 18px; margin-bottom: 20px; line-height: 1.5; }
.modal .options { display: flex; gap: 16px; justify-content: center; }
.modal .option {
  padding: 14px 28px;
  background: var(--tile-bg);
  border-radius: 6px;
  font-size: 22px;
  border: 3px solid transparent;
  min-width: 120px;
}
.modal .option.row-highlight {
  border-color: var(--enji-bright);
  background: var(--col-highlight-bg);
}

.toast {
  position: fixed;
  bottom: 60px;
  right: 20px;
  background: var(--enji);
  color: #fff;
  padding: 12px 20px;
  border-radius: 6px;
  z-index: 200;
  font-size: 16px;
  border: 2px solid var(--enji-light);
  box-shadow: 0 4px 12px rgba(0,0,0,0.5);
}
.toast.hidden { display: none; }

/* PWA インストール促進スナックバー（左下に出るポップアップカード、7 秒で自動消去） */
.install-snackbar {
  position: fixed;
  left: 20px;
  bottom: 80px;
  width: min(420px, calc(100vw - 40px));
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 16px;
  padding: 18px 22px;
  background: var(--enji);
  color: #fff;
  border-radius: 12px;
  z-index: 250;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.55);
  opacity: 0;
  pointer-events: none;
  transform: translateY(16px);
  transition: opacity 240ms ease-out, transform 240ms ease-out;
}
/* 出現 → 表示維持 → 退出 を CSS アニメーションで完結させ、
   モバイル環境で setTimeout が遅延・スキップされても確実に消えるようにする */
.install-snackbar.shown {
  pointer-events: auto;
  animation: install-snackbar-life 7000ms ease-out forwards;
}
@keyframes install-snackbar-life {
  0%   { opacity: 0; transform: translateY(16px); }
  4%   { opacity: 1; transform: translateY(0); }
  93%  { opacity: 1; transform: translateY(0); }
  100% { opacity: 0; transform: translateY(16px); }
}
.install-snackbar-body {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}
.install-snackbar-title {
  font-size: 16px;
  font-weight: bold;
  line-height: 1.2;
}
.install-snackbar-text {
  font-size: 13px;
  color: rgba(255, 255, 255, 0.88);
  line-height: 1.4;
}
.install-snackbar-btn {
  padding: 9px 20px;
  background: #fff;
  color: var(--enji);
  border: none;
  border-radius: 999px;
  font-weight: bold;
  cursor: pointer;
  font-family: inherit;
  font-size: 14px;
  white-space: nowrap;
}
.install-snackbar-btn:hover { background: var(--accent); }
.install-snackbar-btn.hidden { display: none; }
.install-snackbar-close {
  position: absolute;
  top: 6px;
  right: 8px;
  width: 24px;
  height: 24px;
  border: none;
  background: transparent;
  color: rgba(255, 255, 255, 0.7);
  font-size: 18px;
  line-height: 1;
  padding: 0;
  cursor: pointer;
}
.install-snackbar-close:hover { color: #fff; }
@media (max-width: 480px) {
  .install-snackbar {
    left: 12px;
    right: 12px;
    bottom: 70px;
    width: auto;
    padding: 14px 18px;
  }
  .install-snackbar-title { font-size: 15px; }
  .install-snackbar-text { font-size: 12px; }
}

/* Help panel */
.help-content {
  text-align: left;
  font-size: 15px;
  line-height: 1.7;
}
.help-content h3 { color: var(--enji-light); margin: 12px 0 6px; }
.help-content ul { margin-left: 20px; }

@media print {
  .topbar, footer, main, .overlay, .toast, .text-area label { display: none !important; }
  body, html { overflow: visible; }
  #composed {
    border: none;
    font-size: 18px;
    background: #fff;
    color: #000;
    min-height: auto;
    width: 100%;
  }
}
