/*
 * Color palette — ゆるキャラ stationery aesthetic
 * --bg:        #d4e4ed  powder blue background
 * --bg-light:  #e8f0f5  lighter panel bg
 * --card:      #f5f0e8  cream/off-white cards
 * --sidebar:   #c8dce8  slightly deeper blue sidebar
 * --mint:      #9ec5a0  dusty mint green (primary)
 * --pink:      #e8a0aa  blush pink (accent)
 * --yellow:    #e8d88c  pale butter yellow
 * --text:      #3a3a3a  dark text
 * --text-sub:  #7a7a7a  subdued text
 * --border:    #b8c8d4  soft border
 * --outline:   #4a4a4a  black linework anchor
 */

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

body {
  font-family: "Segoe UI", "Hiragino Kaku Gothic ProN", "Meiryo", system-ui, sans-serif;
  background: #d4e4ed;
  color: #3a3a3a;
  min-height: 100vh;
}

/* App layout: sidebar + main */
.app-layout {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  width: 320px;
  min-width: 320px;
  background: #c8dce8;
  border-right: 1px solid #b8c8d4;
  padding: 1rem;
  overflow-y: auto;
  max-height: 100vh;
  position: sticky;
  top: 0;
}

.main-content {
  flex: 1;
  padding: 1rem 1.5rem;
  overflow-y: auto;
  max-height: 100vh;
  position: relative;
}

h1 {
  text-align: center;
  font-size: 1.5rem;
  margin-bottom: 1rem;
  color: #9ec5a0;
}

/* Grade navigation */
.grade-nav {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(2, auto);
  grid-auto-flow: row;
  gap: 0.4rem;
  margin-bottom: 1rem;
}

.grade-btn {
  padding: 0.3rem 0.6rem;
  font-size: 0.85rem;
  border: 2px solid #b8c8d4;
  border-radius: 6px;
  background: #e8f0f5;
  color: #3a3a3a;
  cursor: pointer;
  transition: all 0.2s;
}

.grade-btn:hover {
  border-color: #9ec5a0;
  color: #6a9a6e;
}

.grade-btn.active {
  background: #9ec5a0;
  border-color: #9ec5a0;
  color: white;
}


/* Kanji grid for browsing by grade */
.kanji-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
  justify-content: center;
  margin-bottom: 1rem;
  padding: 0.5rem;
  background: #e8f0f5;
  border-radius: 8px;
  border: 1px solid #b8c8d4;
}

.kanji-grid-btn {
  width: 36px;
  height: 36px;
  font-size: 1.1rem;
  border: 1px solid #b8c8d4;
  border-radius: 4px;
  background: #f5f0e8;
  color: #3a3a3a;
  cursor: pointer;
  transition: all 0.15s;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.kanji-grid-btn:hover {
  background: #e8a0aa;
  border-color: #e8a0aa;
  color: white;
}

.kanji-grid-btn.active {
  background: #e8a0aa;
  border-color: #e8a0aa;
  color: white;
}

/* Input */
.input-section {
  display: flex;
  justify-content: center;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}

#lookupBtn {
  white-space: nowrap;
  width: 60%;
}

/* Loading spinner */
.loading-spinner {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  width: 100%;
  position: absolute;
  top: 0;
  left: 0;
}

.spinner {
  width: 30vmin;
  height: 30vmin;
  border: 10px solid #b8c8d4;
  border-top-color: #9ec5a0;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* Empty state */
.empty-state {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 60vh;
  color: #7a7a7a;
  font-size: 1.2rem;
}

#kanjiInput {
  font-size: 1.3rem;
  padding: 0.4rem 0.5rem;
  width: 40%;
  text-align: center;
  border: 2px solid #b8c8d4;
  border-radius: 8px;
  background: #f5f0e8;
  color: #3a3a3a;
  outline: none;
}

#kanjiInput:focus {
  border-color: #9ec5a0;
}

button {
  padding: 0.5rem 1.2rem;
  font-size: 1rem;
  border: none;
  border-radius: 8px;
  background: #e8a0aa;
  color: white;
  cursor: pointer;
  transition: background 0.2s;
}

button:hover {
  background: #d88a94;
}

.error {
  text-align: center;
  color: #d47a84;
  margin-bottom: 1rem;
  min-height: 1.2em;
}

.hidden {
  display: none !important;
}

.kanji-title {
  text-align: center;
  font-size: 2.5rem;
  margin-bottom: 0.5rem;
  color: #4a4a4a;
}

.section {
  margin-bottom: 0.75rem;
}

.section h3 {
  font-size: 1rem;
  margin-bottom: 0.5rem;
  color: #7a7a7a;
  border-bottom: 1px solid #b8c8d4;
  padding-bottom: 0.3rem;
}

/* Two-column results layout: info left, strokes right */
.results-columns {
  display: flex;
  gap: 2rem;
}

.results-left {
  flex: 1;
  min-width: 0;
}

.results-right {
  flex: 1;
  min-width: 0;
}

/* Stack content columns when main area is narrow */
@media (max-width: 1100px) {
  .results-columns {
    flex-direction: column;
  }
}

/* Sidebar toggle (mobile) */
.sidebar-toggle {
  width: 100%;
  padding: 0.5rem;
  font-size: 0.9rem;
  background: #e8f0f5;
  border: 1px solid #b8c8d4;
  border-radius: 6px;
  color: #4a4a4a;
  cursor: pointer;
  transition: all 0.2s;
}

.sidebar-toggle:hover {
  background: #d4e4ed;
}

/* On narrow screens, move sidebar to top and auto-collapse */
@media (max-width: 768px) {
  .app-layout {
    flex-direction: column;
  }

  .sidebar {
    width: 100%;
    min-width: 100%;
    max-height: none;
    position: relative;
    border-right: none;
    border-bottom: 1px solid #b8c8d4;
  }

  .sidebar-content.collapsed {
    display: none !important;
  }
}

/* Readings */
.readings {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  justify-content: center;
}

.reading-group {
  background: #f5f0e8;
  border-radius: 8px;
  padding: 0.4rem 0.8rem;
  border: 1px solid #b8c8d4;
}

.reading-group .label {
  font-size: 0.65rem;
  color: #7a7a7a;
  margin-bottom: 0.2rem;
}

.reading-group .values {
  font-size: 1rem;
  color: #3a3a3a;
}

.reading-group .values span {
  margin-right: 0.5rem;
}

/* Steps — compact grid that wraps */
.steps-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  justify-content: center;
}

.step-card {
  background: #f5f0e8;
  border-radius: 6px;
  padding: 0.25rem;
  text-align: center;
  border: 1px solid #b8c8d4;
}

.step-card .step-label {
  font-size: 0.6rem;
  color: #7a7a7a;
  margin-top: 0.1rem;
}

.step-card svg {
  display: block;
}

/* Words */
.words-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  justify-content: center;
}

.word-card {
  background: #f5f0e8;
  border-radius: 8px;
  padding: 0.35rem 0.7rem;
  border: 1px solid #b8c8d4;
  text-align: center;
  min-width: 70px;
}

.word-card .word {
  font-size: 1.1rem;
  color: #3a3a3a;
}

.word-card .word-reading {
  font-size: 0.85rem;
  color: #e8a0aa;
  margin-top: 0.15rem;
}

/* Animation */
.animation-container {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
}

.animation-canvas {
  background: #f5f0e8;
  border-radius: 8px;
  border: 1px solid #b8c8d4;
  padding: 0.5rem;
  display: inline-block;
}

.animation-canvas svg {
  display: block;
}


/* Mode header with toggle */
.mode-header {
  margin-bottom: 0.5rem;
}

.mode-toggle-btn {
  display: block;
  width: 100%;
  padding: 0.35rem 0.6rem;
  font-size: 0.8rem;
  border-radius: 6px;
  background: #e8f0f5;
  border: 1px solid #b8c8d4;
  color: #4a4a4a;
  cursor: pointer;
  transition: all 0.2s;
  margin-top: 0.4rem;
}

.mode-toggle-btn:hover {
  background: #9ec5a0;
  border-color: #9ec5a0;
  color: white;
}

/* Tracing mode */
.trace-area {
  text-align: center;
  margin-bottom: 0.75rem;
}

.trace-canvas {
  background: #f5f0e8;
  border-radius: 8px;
  border: 2px solid #b8c8d4;
  padding: 0.5rem;
  display: inline-block;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.trace-canvas svg {
  display: block;
}

.trace-canvas.error {
  border-color: #e8a0aa;
  transition: border-color 0.15s;
}

.trace-info {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  margin-top: 0.5rem;
}

.trace-counter {
  font-size: 0.9rem;
  color: #7a7a7a;
}

.trace-retry-btn {
  padding: 0.3rem 0.8rem;
  font-size: 0.8rem;
  border-radius: 6px;
  background: #e8f0f5;
  border: 1px solid #b8c8d4;
}

.trace-retry-btn:hover {
  background: #9ec5a0;
  border-color: #9ec5a0;
  color: white;
}

@keyframes trace-pulse {
  0%, 100% { opacity: 1; r: 7; }
  50% { opacity: 0.5; r: 9; }
}

.trace-message {
  margin-top: 0.5rem;
  font-size: 1.2rem;
  color: #9ec5a0;
  font-weight: bold;
}

/* Practice options */
.practice-options {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 1rem;
  justify-content: center;
}

.practice-options label {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.9rem;
  color: #7a7a7a;
}

.practice-options input[type="number"] {
  width: 50px;
  padding: 0.3rem;
  background: #f5f0e8;
  border: 1px solid #b8c8d4;
  border-radius: 6px;
  color: #3a3a3a;
  text-align: center;
}

/* Print styles — Japanese school practice sheet */
@media print {
  @page {
    size: landscape;
    margin: 8mm;
  }

  body {
    background: white !important;
    color: black !important;
  }

  .app-layout {
    display: none !important;
  }

  .print-sheet {
    display: block !important;
    width: 100%;
    font-family: "Hiragino Kaku Gothic ProN", "Meiryo", "Yu Gothic", sans-serif;
  }

  /* --- Header bar --- */
  .ps-header {
    display: flex;
    align-items: center;
    gap: 1rem;
    margin-bottom: 8px;
    border-bottom: 3px solid #e8d88c;
    padding-bottom: 6px;
  }

  .ps-grade-badge {
    background: #e8a0aa;
    color: white;
    font-size: 1.1rem;
    font-weight: bold;
    padding: 4px 12px;
    border-radius: 6px;
    white-space: nowrap;
  }

  .ps-title {
    display: flex;
    flex-direction: column;
  }

  .ps-title-main {
    font-size: 1rem;
    font-weight: bold;
    color: #9ec5a0;
  }

  .ps-title-sub {
    font-size: 0.8rem;
    color: #666;
  }

  .ps-name-fields {
    margin-left: auto;
    display: flex;
    gap: 0.5rem;
    font-size: 0.9rem;
  }

  .ps-name-field {
    border-bottom: 1px solid #333;
    padding: 0 2rem 2px 0.3rem;
  }

  /* --- Main 3-column layout --- */
  .ps-main {
    display: flex;
    gap: 8px;
    height: calc(100vh - 60px);
  }

  .ps-left {
    flex: 3;
    display: flex;
    gap: 4px;
  }

  .ps-center {
    flex: 1;
    display: flex;
    gap: 4px;
  }

  .ps-right {
    flex: 1.2;
    display: flex;
    flex-direction: column;
    gap: 6px;
  }

  /* --- Practice grid cells --- */
  .ps-practice-grid,
  .ps-stroke-steps {
    border-collapse: collapse;
    width: 100%;
    height: 100%;
    table-layout: fixed;
  }

  .ps-cell {
    border: 1px solid #aaa;
    position: relative;
    aspect-ratio: 1;
    padding: 0;
    vertical-align: middle;
    text-align: center;
  }

  /* Cross guide lines in every cell */
  .ps-cell::before,
  .ps-cell::after {
    content: "";
    position: absolute;
  }

  .ps-cell::before {
    top: 0;
    bottom: 0;
    left: 50%;
    width: 1px;
    background: #d0e8ff;
  }

  .ps-cell::after {
    left: 0;
    right: 0;
    top: 50%;
    height: 1px;
    background: #d0e8ff;
  }

  .ps-cell svg {
    position: relative;
    z-index: 1;
  }

  .ps-stroke-cell svg {
    position: relative;
    z-index: 1;
  }

  /* Vertical labels */
  .ps-vertical-label {
    writing-mode: vertical-rl;
    font-size: 0.7rem;
    color: #333;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 2px;
    white-space: nowrap;
  }

  /* --- Right panel --- */
  .ps-ref-kanji {
    border: 3px solid #e8a0aa;
    border-radius: 8px;
    aspect-ratio: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 6px;
    position: relative;
  }

  .ps-ref-kanji svg {
    width: 100%;
    height: 100%;
  }

  .ps-stroke-count {
    text-align: center;
    font-size: 1rem;
    font-weight: bold;
    color: #333;
  }

  .ps-reading-table {
    border-collapse: collapse;
    width: 100%;
    font-size: 0.75rem;
  }

  .ps-reading-table th {
    background: #9ec5a0;
    color: white;
    padding: 3px 6px;
    text-align: center;
    font-size: 0.8rem;
  }

  .ps-reading-label {
    background: #e8f0f5;
    text-align: center;
    padding: 2px 4px;
    font-weight: bold;
    border: 1px solid #ccc;
  }

  .ps-reading-value {
    text-align: center;
    padding: 4px;
    border: 1px solid #ccc;
    color: #6a9a6e;
    font-size: 0.8rem;
  }
}
