/* ========================================
   ずけい れんしゅう — Global Styles
   ======================================== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --primary:   #FF6B6B;
  --primary2:  #FF8E53;
  --correct:   #4CAF50;
  --wrong:     #FF5252;
  --bg:        #FFF9F0;
  --panel-bg:  #FFFFFF;
  --dot-color: #AACCEE;
  --line-color:#1A73E8;
  --hint-line: #BDD7FB;
  --grid-line: #DDE8F5;
  --text-main: #333355;
  --text-sub:  #667799;
  --shadow:    0 4px 16px rgba(0,0,0,.10);
  --radius:    16px;
  --font:      'M PLUS Rounded 1c', 'Kosugi Maru', sans-serif;
  --header-size: 56px;
}

html {
  height: 100%;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text-main);
}

body {
  min-height: 100%;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text-main);
}

/* ===== Screen management ===== */
.screen { display: none; width: 100%; min-height: 100vh; overflow-y: auto; }
.screen.active {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
}

/* ===== Buttons ===== */
button {
  cursor: pointer;
  font-family: var(--font);
  border: none;
  outline: none;
  transition: transform .1s, box-shadow .1s;
}
button:active { transform: scale(.95); }

.btn-start {
  margin-top: 28px;
  padding: 16px 56px;
  font-size: 1.4rem;
  font-weight: 800;
  color: #fff;
  background: linear-gradient(135deg, var(--primary), var(--primary2));
  border-radius: 50px;
  box-shadow: 0 6px 20px rgba(255,107,107,.45);
  letter-spacing: .05em;
}
.btn-start:disabled { opacity: .4; pointer-events: none; }
.btn-start:hover:not(:disabled) {
  box-shadow: 0 8px 28px rgba(255,107,107,.55);
  transform: translateY(-2px);
}

.btn-primary {
  padding: 0.55em 1.6em;
  font-size: 1.65rem;
  font-weight: 700;
  color: #fff;
  background: linear-gradient(135deg, var(--primary), var(--primary2));
  border-radius: 50px;
  box-shadow: 0 4px 12px rgba(255,107,107,.4);
  white-space: nowrap;
}
.btn-primary:hover { transform: translateY(-1px); }

.btn-sub {
  padding: 0.5em 1.4em;
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--text-main);
  background: #EEF3FB;
  border-radius: 50px;
  box-shadow: 0 2px 8px rgba(0,0,0,.08);
  white-space: nowrap;
}
.btn-sub:hover { background: #DDE8F5; }

.btn-icon {
  width: 40px; height: 40px;
  border-radius: 50%;
  background: #EEF3FB;
  display: flex; align-items: center; justify-content: center;
  font-size: 1rem;
  color: var(--text-sub);
  flex-shrink: 0;
}

.btn-text-small {
  background: none;
  color: var(--text-sub);
  font-size: .85rem;
  text-decoration: underline dotted;
  padding: 4px 0;
  margin-top: 8px;
}

.btn-small {
  padding: 6px 16px;
  font-size: .85rem;
  font-weight: 700;
  color: #fff;
  background: var(--primary);
  border-radius: 20px;
}

/* ===== START SCREEN ===== */
.start-wrapper {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 32px 24px 48px;
  max-width: 540px;
  width: 100%;
}

.start-cat {
  width: 140px; height: 140px;
  object-fit: contain;
  animation: bounce 2s infinite ease-in-out;
  filter: drop-shadow(0 8px 16px rgba(0,0,0,.15));
}

@keyframes bounce {
  0%, 100% { transform: translateY(0); }
  50%       { transform: translateY(-12px); }
}

.start-title {
  font-size: 2rem;
  font-weight: 800;
  margin-top: 16px;
  color: var(--primary);
  letter-spacing: .08em;
}
.start-sub { font-size: 1rem; color: var(--text-sub); margin-top: 6px; }

.level-select { width: 100%; margin-top: 28px; }
.level-label  { font-size: .95rem; color: var(--text-sub); margin-bottom: 12px; text-align: center; }
.level-buttons { display: flex; flex-direction: column; gap: 12px; }

.level-btn {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 16px 20px;
  background: var(--panel-bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border: 3px solid transparent;
  transition: border-color .2s, box-shadow .2s;
  text-align: left;
  width: 100%;
}
.level-btn.selected,
.level-btn:hover {
  border-color: var(--primary);
  box-shadow: 0 4px 20px rgba(255,107,107,.25);
}

.lv-icon { font-size: 1.2rem; min-width: 56px; text-align: center; }
.lv-name { font-size: 1.05rem; font-weight: 800; color: var(--text-main); flex: 1; }
.lv-desc { font-size: .8rem; color: var(--text-sub); white-space: nowrap; }

/* ===== API Key ===== */
.api-section { width: 100%; margin-top: 20px; text-align: center; }
#api-key-panel {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  justify-content: center;
  margin-top: 8px;
  padding: 12px 16px;
  background: var(--panel-bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
#input-api-key {
  flex: 1 1 200px;
  padding: 8px 12px;
  border: 2px solid #DDE8F5;
  border-radius: 24px;
  font-size: .9rem;
  font-family: var(--font);
  outline: none;
  min-width: 0;
}
#input-api-key:focus { border-color: var(--primary); }
.api-note { width: 100%; font-size: .75rem; color: var(--text-sub); margin-top: 4px; }

.admin-model-btn { color: #1A73E8; margin-top: 6px; }

/* ===== GAME SCREEN ===== */
#screen-game { overflow-y: auto; }

.game-header {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  background: var(--panel-bg);
  border-bottom: 2px solid #EEF3FB;
  position: sticky;
  top: 0;
  z-index: 10;
  align-self: stretch;
}

.progress-area { flex: 1; display: flex; flex-direction: column; gap: 4px; min-width: 0; }
#progress-text { font-size: .85rem; font-weight: 700; color: var(--text-sub); }
.progress-bar-bg { width: 100%; height: 8px; background: #DDE8F5; border-radius: 8px; overflow: hidden; }
.progress-bar-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--primary), var(--primary2));
  border-radius: 8px;
  transition: width .4s ease;
}
.score-area { font-size: 1rem; font-weight: 800; color: var(--primary); white-space: nowrap; }

.game-main {
  width: 100%;
  max-width: 1000px;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 12px 16px 32px;
  gap: 16px;
  flex: 1;
}

.problem-row { display: flex; gap: 24px; width: 100%; justify-content: center; }
.panel { display: flex; flex-direction: column; align-items: center; gap: 6px; flex: 1 1 0; min-width: 0; }
.panel-label { font-size: 1.1rem; font-weight: 800; color: var(--text-sub); letter-spacing: .05em; }

/* グリッドヘッダー */
.grid-outer { display: flex; flex-direction: column; width: 100%; align-items: flex-start; }
.col-header { display: flex; padding-left: var(--header-size); width: 100%; margin-bottom: 2px; }
.col-header-cell {
  flex: 1;
  text-align: center;
  font-size: 1.4rem;
  font-weight: 800;
  color: #5588CC;
  line-height: var(--header-size);
}
.grid-body { display: flex; flex-direction: row; width: 100%; align-items: flex-start; }
.row-header { display: flex; flex-direction: column; width: var(--header-size); flex-shrink: 0; }
.row-header-cell { flex: 1; display: flex; align-items: center; justify-content: center; font-size: 1.8rem; line-height: 1; }

.canvas-wrap {
  position: relative;
  background: var(--panel-bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  border: 3px solid #DDE8F5;
  flex: 1 1 0;
  min-width: 0;
  aspect-ratio: 1 / 1;
}
.canvas-wrap canvas {
  position: absolute;
  top: 0; left: 0;
  width: 100% !important;
  height: 100% !important;
}
#canvas-model   { z-index: 1; }
#canvas-answer  { z-index: 1; }
/* 差分7修正: pointer-events は JS(setupInteraction)で制御するためCSS側では設定しない */
#canvas-overlay { z-index: 2; cursor: crosshair; }

.hint-msg {
  font-size: 1.2rem;
  font-weight: 700;
  color: #1A73E8;
  background: #EAF2FF;
  padding: 10px 28px;
  border-radius: 20px;
  text-align: center;
  white-space: nowrap;
}

.action-row {
  display: flex;
  gap: 12px;
  align-items: center;
  flex-wrap: nowrap;
  justify-content: center;
  width: 100%;
  overflow-x: auto;
}
.action-row .btn-sub,
.action-row .btn-primary { flex: 0 0 auto; white-space: nowrap; }

/* ===== FEEDBACK OVERLAY ===== */
.feedback-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 16px;
  overflow-y: auto;
}
.feedback-box {
  background: var(--panel-bg);
  border-radius: 24px;
  padding: 28px 24px;
  max-width: 440px;
  width: 100%;
  text-align: center;
  box-shadow: 0 12px 40px rgba(0,0,0,.25);
  animation: popIn .3s cubic-bezier(.34,1.56,.64,1);
  max-height: 90vh;
  overflow-y: auto;
}
@keyframes popIn {
  from { transform: scale(.6); opacity: 0; }
  to   { transform: scale(1);  opacity: 1; }
}
.fb-title { font-size: 1.4rem; font-weight: 800; margin-bottom: 8px; }
.fb-title.wrong   { color: var(--wrong); }
.fb-title.correct { color: var(--correct); }
.fb-desc { font-size: .95rem; color: var(--text-sub); margin-bottom: 12px; }
#canvas-wrong {
  width: 100% !important; height: auto !important;
  border-radius: var(--radius);
  border: 2px solid #DDE8F5;
  display: block;
  max-width: 280px;
  margin: 0 auto 12px;
}
.gotit-img {
  width: 140px; height: 140px;
  object-fit: contain;
  animation: spinBounce .5s cubic-bezier(.34,1.56,.64,1);
  margin-bottom: 8px;
}
@keyframes spinBounce {
  from { transform: scale(.3) rotate(-30deg); opacity: 0; }
  to   { transform: scale(1)  rotate(0deg);  opacity: 1; }
}
.mt16 { margin-top: 16px; }

/* ===== RESULT SCREEN ===== */
.result-wrapper { display: flex; flex-direction: column; align-items: center; padding: 40px 24px 48px; max-width: 440px; width: 100%; }
.result-cat { width: 140px; height: 140px; object-fit: contain; animation: bounce 2s infinite; }
.result-title { font-size: 1.8rem; font-weight: 800; color: var(--primary); margin-top: 16px; }
.result-score-label { font-size: 1rem; color: var(--text-sub); margin-top: 20px; }
.result-score { font-size: 3rem; font-weight: 800; color: var(--text-main); margin-top: 4px; }
.result-msg { font-size: 1rem; color: var(--text-sub); margin-top: 12px; text-align: center; line-height: 1.7; }
.result-buttons { display: flex; gap: 16px; margin-top: 32px; flex-wrap: wrap; justify-content: center; }

/* ===== LOADING ===== */
.loading-overlay {
  position: fixed; inset: 0;
  background: rgba(255,249,240,.85);
  display: flex; align-items: center; justify-content: center;
  z-index: 200;
}
.loading-box { display: flex; flex-direction: column; align-items: center; gap: 16px; font-size: 1.1rem; color: var(--text-sub); font-weight: 700; }
.spinner { width: 48px; height: 48px; border: 5px solid #DDE8F5; border-top-color: var(--primary); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ===== Utility ===== */
.hidden { display: none !important; }

/* ===== エラー・通知バナー ===== */
.model-error-banner {
  position: fixed;
  top: 0; left: 0; right: 0;
  padding: 10px 20px;
  font-size: 13px;
  font-family: var(--font);
  font-weight: 700;
  text-align: center;
  cursor: pointer;
  z-index: 9999;
  transition: background .3s;
  line-height: 1.5;
}
.model-error-banner--error { background: #c62828; color: #fff; }
.model-error-banner--error:hover { background: #b71c1c; }
.model-error-banner--info  { background: #1565c0; color: #fff; }
.model-error-banner--info:hover { background: #0d47a1; }

.banner-close-btn {
  background : none;
  border     : none;
  color      : inherit;
  font-size  : 1.1rem;
  cursor     : pointer;
  margin-left: 0.75rem;
  padding    : 0 0.25rem;
  line-height: 1;
  opacity    : 0.8;
  flex-shrink: 0;
}
.banner-close-btn:hover {
  opacity: 1;
}
.model-error-banner {
  display    : flex;           /* ← textとボタンを横並びに */
  align-items: center;
  justify-content: space-between;
}


/* ===== Responsive — タブレット ===== */
@media (min-width: 601px) and (max-width: 900px) {
  :root { --header-size: 44px; }
  .problem-row { gap: 16px; }
  .action-row { gap: 10px; }
  .action-row .btn-sub     { font-size: 1.25rem; }
  .action-row .btn-primary { font-size: 1.35rem; }
}

/* ===== Responsive — スマートフォン ===== */
@media (max-width: 600px) {
  .start-cat   { width: 100px; height: 100px; }
  .start-title { font-size: 1.5rem; }
  .start-sub   { font-size: .9rem; }
  .btn-start   { font-size: 1.1rem; padding: 13px 36px; }
  .lv-desc     { display: none; }

  .problem-row { flex-direction: column; align-items: center; gap: 16px; }
  .panel       { width: 100%; max-width: 380px; }

  .game-header   { padding: 8px 12px; gap: 8px; }
  #progress-text { font-size: .75rem; }
  .score-area    { font-size: .9rem; }
  .game-main     { padding: 8px 8px 24px; gap: 10px; }

  :root { --header-size: 36px; }

  .action-row { gap: 6px; }
  .action-row .btn-sub     { font-size: 1rem;    padding: 0.5em 1em; }
  .action-row .btn-primary { font-size: 1.05rem; padding: 0.5em 1.1em; }

  .hint-msg    { font-size: 1rem; padding: 8px 16px; white-space: normal; }
  #canvas-wrong { max-width: 220px; }
  .gotit-img   { width: 110px; height: 110px; }
  .fb-title    { font-size: 1.2rem; }
  .feedback-box { padding: 20px 16px; }

  .result-cat   { width: 110px; height: 110px; }
  .result-title { font-size: 1.5rem; }
  .result-score { font-size: 2.4rem; }

  .model-error-banner { font-size: 11px; padding: 8px 12px; }
}

/* ===== 極小画面 ===== */
@media (max-width: 360px) {
  .start-title { font-size: 1.3rem; }
  :root { --header-size: 28px; }
  .action-row { gap: 4px; }
  .action-row .btn-sub     { font-size: .85rem; padding: 0.45em 0.8em; }
  .action-row .btn-primary { font-size: .9rem;  padding: 0.45em 0.9em; }
}
