* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: #f5f5f4;
  color: #292524;
  font-family: Inter, "Segoe UI", "Microsoft YaHei", system-ui, sans-serif;
  touch-action: manipulation;
}

button, input, textarea {
  font: inherit;
}

/* Layout container */
.app-container {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

/* Top Nav */
.top-nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 32px;
  margin-bottom: 24px;
}
.nav-left { display: flex; align-items: center; gap: 12px; }
.brand-logo { display: flex; align-items: center; gap: 8px; font-size: 28px; font-weight: 800; font-style: italic; letter-spacing: -0.05em; }
.brand-name { font-family: Georgia, serif; }
.brand-subtitle { color: #78716c; font-size: 14px; margin-left: 12px; }
.brand-badge { background: #dcfce7; color: #166534; font-size: 10px; font-weight: 800; padding: 4px 8px; border-radius: 4px; margin-left: 8px; }
.nav-right { display: flex; align-items: center; gap: 16px; font-size: 14px; color: #44403c; flex-wrap: wrap; justify-content: flex-end; }
.fake-control { display: flex; align-items: center; gap: 6px; cursor: pointer; }
.fake-select { border: 1px solid #d6d3d1; border-radius: 6px; padding: 6px 12px; background: white; display: flex; align-items: center; gap: 12px; font-weight: 600; }

/* Main Content */
.main-content {
  display: flex;
  justify-content: center;
  gap: 40px;
  padding: 0 32px 40px;
  max-width: 1400px;
  margin: 0 auto;
  width: 100%;
}

/* Center Board area */
.center-board {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.wood-texture {
  --board-size: 560px;
  position: relative;
  padding: 32px 16px 16px 32px;
  border-radius: 12px;
  background-color: #e5ccab;
  background-image:
    linear-gradient(90deg, rgba(255,255,255,0.1) 0%, rgba(0,0,0,0.05) 100%),
    url("data:image/svg+xml,%3Csvg width='100' height='100' viewBox='0 0 100 100' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='noise'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.6' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100' height='100' filter='url(%23noise)' opacity='0.08'/%3E%3C/svg%3E");
  box-shadow: inset 2px 2px 5px rgba(255,255,255,0.3), inset -2px -2px 5px rgba(0,0,0,0.1), 0 25px 50px -18px rgb(0 0 0 / 0.45);
  user-select: none;
}
.files-row {
  position: absolute;
  top: 8px;
  left: 32px;
  width: var(--board-size);
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  color: rgba(41, 37, 36, 0.85);
  font-size: 16px;
  font-weight: 900;
  text-align: center;
  pointer-events: none;
}
.ranks-col {
  position: absolute;
  top: 32px;
  left: 8px;
  height: var(--board-size);
  display: grid;
  grid-template-rows: repeat(8, 1fr);
  align-items: center;
  color: rgba(41, 37, 36, 0.85);
  font-size: 16px;
  font-weight: 900;
  text-align: center;
  pointer-events: none;
}
.board-grid {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  width: var(--board-size);
  height: var(--board-size);
  gap: 2px;
  padding: 2px;
  border-radius: 4px;
  background-color: #2f4f3e;
  box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.3);
  box-sizing: border-box;
}
.cell {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  border: 0;
  padding: 0;
  background-color: #5c8d73;
  box-shadow: inset 1px 1px 3px rgba(0,0,0,0.2);
  cursor: pointer;
  overflow: hidden;
}
.cell:hover { background-color: #6a9b81; }
.disc {
  width: 85%; height: 85%; border-radius: 50%; pointer-events: none; z-index: 10;
}
.disc.black {
  background: radial-gradient(circle at 35% 35%, #555, #1a1a1a, #000);
  box-shadow: 2px 2px 4px rgba(0,0,0,0.6), inset 1px 1px 2px rgba(255,255,255,0.2);
}
.disc.white {
  background: radial-gradient(circle at 35% 35%, #fff, #f0f0f0, #d1d1d1);
  box-shadow: 2px 2px 4px rgba(0,0,0,0.4), inset -1px -1px 2px rgba(0,0,0,0.1);
}
.cell.legal::after {
  content: ""; width: 25%; height: 25%; border-radius: 50%;
  background-color: rgba(0, 0, 0, 0.2); box-shadow: inset 0 0 2px rgba(0,0,0,0.2);
}
.cell:hover.legal::after {
  background-color: rgba(0, 0, 0, 0.4); transform: scale(1.2); transition: all 0.2s;
}
.eval-score {
  position: absolute; inset: 0; z-index: 30;
  display: flex; align-items: center; justify-content: center;
  padding: 4px; box-sizing: border-box;
  font-weight: 900; font-size: 26px; line-height: 1;
  white-space: nowrap;
  text-shadow: -1px -1px 0 rgba(0,0,0,0.8), 1px -1px 0 rgba(0,0,0,0.8), -1px 1px 0 rgba(0,0,0,0.8), 1px 1px 0 rgba(0,0,0,0.8), 0 2px 4px rgba(0,0,0,0.5);
  pointer-events: none; font-family: "Arial Black", "Inter", sans-serif;
  letter-spacing: -0.05em;
}
.eval-score[data-mode="eval"], .eval-score[data-mode="winrate"] {
  font-size: 22px;
  letter-spacing: -0.02em;
}
.score-best {
  color: #3b82f6; transform: scale(1.15);
  text-shadow: -1px -1px 0 #fff, 1px -1px 0 #fff, -1px 1px 0 #fff, 1px 1px 0 #fff, 0 2px 4px rgba(0,0,0,0.5);
}
.score-normal {
  color: #fbbf24;
}

details.group[open] .group-open-rotate { transform: rotate(180deg); }
details.group[open] summary { border-bottom-color: #f5f5f4 !important; }

.board-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  margin-top: 16px;
}

.status-pill {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 8px 20px; border: 1px solid #e7e5e4; border-radius: 999px;
  font-size: 12px; font-weight: 800; box-shadow: 0 1px 2px rgb(0 0 0 / 0.06);
  background: white;
}
.status-idle { background-color: #e7e5e4; color: #57534e; }
.status-done { background-color: #dcfce7; color: #166534; }
.status-out { background-color: #fef3c7; color: #92400e; }

.nav-controls {
  display: flex;
  align-items: center;
  gap: 16px;
}
.nav-controls input[type="range"] { width: 120px; accent-color: #44403c; }
.nav-buttons { display: flex; gap: 8px; }
.nav-buttons button {
  border: 1px solid #d6d3d1; background: #fff; color: #57534e;
  border-radius: 8px; padding: 6px 12px; font-weight: 800; cursor: pointer;
}
.move-counter {
  color: #78716c; font-family: "JetBrains Mono", Consolas, monospace; font-size: 12px; font-weight: 700;
}

/* Right Rail */
.right-rail {
  flex: 0 0 360px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.stone-card {
  background: white; border-radius: 12px; border: 1px solid #e7e5e4;
  box-shadow: 0 1px 2px rgb(0 0 0 / 0.05); overflow: hidden;
}
.card-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 14px 16px; border-bottom: 1px solid #f5f5f4;
}
.card-title {
  display: flex; align-items: center; gap: 8px; font-size: 12px; font-weight: 800;
  letter-spacing: 0.05em; color: #1c1917;
}
.card-header-right { font-size: 11px; color: #78716c; font-weight: 700; }
.card-body { padding: 16px; }
.card-body.p-0 { padding: 0; }
.card-footer {
  padding: 12px 16px; background: #fafaf9; border-top: 1px solid #f5f5f4;
  font-size: 12px; font-weight: 700; display: flex; justify-content: space-between;
}

/* Buttons and Inputs */
.sonetto-button {
  border: 1px solid #d6d3d1; border-radius: 8px; padding: 9px 10px;
  text-align: center; font-weight: 800; font-size: 12px; cursor: pointer;
  display: inline-block; text-decoration: none; box-sizing: border-box;
}
.sonetto-button.dark { border-color: #292524; background: #292524; color: #fff; }
.sonetto-button.light { background: #fff; color: #57534e; }
.sonetto-button.full { display: block; width: 100%; }
.text-button {
  background: none; border: none; color: #3b82f6; font-size: 11px;
  font-weight: 800; cursor: pointer; padding: 0; text-transform: uppercase;
}
.input-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}
.copy-button {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  text-transform: none;
}
.update-button {
  color: #57534e;
  text-transform: none;
}
.update-button:hover {
  color: #1c1917;
}
.clear-button {
  color: #a8a29e;
  text-transform: none;
}
.clear-button:hover {
  color: #ef4444;
}
.button-icon {
  display: inline-flex;
  width: 12px;
  height: 12px;
}
.button-icon svg {
  width: 100%;
  height: 100%;
  fill: currentColor;
}
.text-center { text-align: center; }
.mt-2 { margin-top: 12px; }

.core-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.mini-progress {
  height: 6px; margin-top: 12px; overflow: hidden; border-radius: 999px; background: #e7e5e4;
}
.mini-progress-fill { width: 0; height: 100%; background: #0f766e; transition: width 0.25s ease; }
.book-status-label { margin-top: 8px; font-size: 11px; color: #78716c; font-weight: 700; text-align: center; text-transform: uppercase; }

.settings-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.settings-group { margin-bottom: 12px; }
.settings-label {
  display: flex; justify-content: space-between; margin-bottom: 6px;
  color: #57534e; font-size: 10px; font-weight: 800; letter-spacing: 0.05em; text-transform: uppercase;
}
.settings-value { color: #0369a1; font-family: "JetBrains Mono", Consolas, monospace; }
.settings-number-input, .move-input {
  width: 100%; border: 1px solid #d6d3d1; border-radius: 10px; padding: 9px 10px;
  background: #fff; color: #1c1917; font-family: "JetBrains Mono", Consolas, monospace;
  font-size: 12px; font-weight: 700; outline: none; transition: border-color 0.2s;
}
.settings-number-input:focus, .move-input:focus { border-color: #0369a1; }
.move-input {
  min-height: 96px;
  resize: vertical;
  background: #fafaf9;
  color: #57534e;
}
.load-game-button {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.side-badge { color: #1c1917; font-weight: 800; }
.best-value-label { color: #78716c; font-family: "JetBrains Mono", Consolas, monospace; }
#bestValue { color: #2563eb; font-weight: 900; }

.analysis-results {
  display: grid; gap: 0; max-height: 480px; overflow-y: auto; background: #fafaf9;
}
.move-row {
  display: grid; grid-template-columns: 42px 1fr 70px; gap: 8px; align-items: center;
  width: 100%; border: none; border-bottom: 1px solid #e7e5e4; border-left: 4px solid transparent;
  padding: 12px 16px; background: #fff; color: #292524; text-align: left; cursor: pointer;
  transition: background 0.2s;
}
.move-row:hover { background: #f5f5f4; }
.move-row:last-child { border-bottom: none; }
.move-row.best { border-left-color: #4b5563; background: #f3f4f6; }
.move-coord { font-size: 16px; font-weight: 900; }
.move-detail { color: #78716c; font-size: 11px; line-height: 1.4; }
.move-value {
  color: #2563eb; font-family: "JetBrains Mono", Consolas, monospace;
  font-size: 13px; font-weight: 900; text-align: right;
}
.empty-state { padding: 32px 16px; color: #78716c; font-size: 13px; text-align: center; }
.data-note {
  align-self: flex-end;
  padding: 0 4px;
  color: #a8a29e;
  font-size: 11px;
  line-height: 1.4;
}

/* Custom scroll */
.custom-scroll::-webkit-scrollbar { width: 4px; }
.custom-scroll::-webkit-scrollbar-track { background: transparent; }
.custom-scroll::-webkit-scrollbar-thumb { background: #d6d3d1; border-radius: 2px; }

/* Dialog */
.book-dialog {
  width: min(340px, calc(100vw - 40px)); border: 0; border-radius: 12px;
  box-shadow: 0 20px 25px -5px rgb(0 0 0 / 0.18);
}
.book-dialog form { padding: 22px; text-align: center; }
.book-dialog h2 { margin: 0; font-size: 20px; }
.book-dialog p { margin: 10px 0 18px; color: #78716c; }
.book-dialog::backdrop { background: rgb(0 0 0 / 0.45); }
.book-dialog button {
  border: 1px solid #292524; background: #292524; color: #fff;
  border-radius: 8px; padding: 9px 10px; font-weight: 800; font-size: 12px; cursor: pointer;
}
.mandatory-dialog::backdrop {
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

@media (max-width: 1024px) {
  .main-content {
    flex-direction: column;
    align-items: center;
    gap: 20px;
    padding-left: 16px;
    padding-right: 16px;
  }
  .center-board {
    width: 100%;
  }
  .wood-texture {
    --board-size: min(560px, calc(100vw - 32px));
    padding: 0;
    border-radius: 6px;
    background: transparent;
    box-shadow: 0 18px 35px -22px rgb(0 0 0 / 0.55);
  }
  .files-row,
  .ranks-col {
    display: none;
  }
  .right-rail {
    width: 100%;
    max-width: 560px;
    flex: 0 0 auto;
    margin-top: 0;
  }
}

@media (max-width: 640px) {
  .main-content {
    padding: 0 6px 32px;
  }
  .wood-texture {
    --board-size: min(560px, calc(100vw - 12px));
  }
  .top-nav {
    flex-direction: column;
    gap: 16px;
    padding: 16px;
  }
  .nav-right {
    justify-content: center;
  }
  .right-rail {
    max-width: 100%;
  }
  .data-note {
    align-self: flex-start;
  }
}
