:root {
  color-scheme: light;
  --bg: #f6f7fb;
  --panel: #ffffff;
  --ink: #20242d;
  --muted: #6b7280;
  --line: #dfe3ea;
  --accent: #0f8f7b;
  --accent-dark: #08705f;
  --danger: #d55244;
  --gold: #e2a52d;
  --light-square: #ede7d0;
  --dark-square: #6f9a71;
  --focus: 0 0 0 3px rgba(15, 143, 123, 0.25);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

html {
  -webkit-text-size-adjust: 100%;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
  -webkit-tap-highlight-color: transparent;
}

input,
button,
select {
  font: inherit;
}

button {
  border: 0;
}

a:focus-visible,
input:focus-visible,
button:focus-visible,
select:focus-visible {
  outline: none;
  box-shadow: var(--focus);
}

button,
a,
.square,
.board-grid,
.icon-action,
.coffee-link,
.state-toggle,
.piece-button,
.toggle-button {
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}

.app-shell {
  width: min(780px, 100%);
  min-height: 100vh;
  margin: 0 auto;
  padding: calc(12px + env(safe-area-inset-top)) 14px calc(18px + env(safe-area-inset-bottom));
  display: grid;
  grid-template-rows: auto 1fr;
  gap: 8px;
}

.topbar {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: start;
  gap: 12px;
  padding: 4px 2px;
}

.title-block {
  min-width: 0;
  display: grid;
  align-content: start;
}

h1,
h2,
p {
  margin: 0;
}

.brand-logo {
  display: flex;
  align-items: center;
  gap: 10px;
  width: min(430px, 100%);
  margin-bottom: 10px;
  line-height: 1;
  white-space: nowrap;
}

.brand-ink {
  fill: #15171a;
}

.brand-paper {
  fill: #f8fafc;
}

.brand-accent {
  fill: var(--accent);
}

.brand-accent-stroke {
  fill: none;
  stroke: var(--accent);
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 10;
}

.brand-mark {
  flex: 0 0 auto;
  width: 82px;
  height: auto;
  overflow: visible;
}

.brand-name {
  fill: #15171a;
  color: #15171a;
  min-width: 0;
  font-size: clamp(1.35rem, 3.2vw, 1.65rem);
  font-weight: 850;
  letter-spacing: 0;
}

.brand-dotcom {
  color: var(--accent);
}

h1 {
  font-size: clamp(1.45rem, 7vw, 2.15rem);
  line-height: 1;
  letter-spacing: 0;
}

.tagline {
  margin-top: 0;
  color: var(--muted);
  font-size: clamp(0.86rem, 3.8vw, 1rem);
  font-weight: 700;
  line-height: 1.2;
}

.tagline-row {
  display: flex;
  align-items: baseline;
  gap: 7px;
  min-width: 0;
}

h2 {
  font-size: 1rem;
  line-height: 1.2;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  box-shadow: 0 12px 34px rgba(32, 36, 45, 0.06);
}

.section-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
}

.source-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  width: auto;
}

.coffee-link {
  display: block;
  min-width: 0;
  width: 207px;
  height: 44px;
  line-height: 1;
  border-radius: 8px;
  overflow: hidden;
  text-decoration: none;
}

.coffee-link img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.coffee-link:active {
  transform: translateY(1px);
}

.icon-action {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
}

.icon-action svg {
  width: 23px;
  height: 23px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.info-action {
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  width: 22px;
  height: 22px;
  border-radius: 999px;
  background: var(--accent);
  color: #ffffff;
  cursor: pointer;
  transform: translateY(-1px);
}

.info-action span {
  display: block;
  width: auto;
  height: auto;
  border: 0;
  font-size: 17px;
  font-family: Georgia, "Times New Roman", serif;
  font-style: italic;
  font-weight: 700;
  line-height: 1;
  text-align: center;
  transform: translate(-0.5px, -0.5px);
}

.icon-action:active,
.info-action:active,
.state-toggle:active {
  transform: translateY(1px);
}

body.has-modal {
  overflow: hidden;
}

.info-overlay {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(33, 38, 49, 0.48);
}

.info-overlay[hidden] {
  display: none;
}

.info-card {
  width: min(100%, 520px);
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  box-shadow: 0 18px 45px rgba(20, 25, 35, 0.18);
}

.info-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}

.info-card-header h2 {
  font-size: 1.05rem;
}

.info-close-button {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border-radius: 8px;
  color: var(--muted);
  background: #f4f6f9;
  cursor: pointer;
}

.info-close-button svg,
.info-thumbnail svg {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.info-steps {
  display: grid;
  gap: 14px;
}

.info-step {
  display: grid;
  grid-template-columns: 24px 86px minmax(0, 1fr);
  column-gap: 18px;
  row-gap: 6px;
  align-items: center;
}

.info-step-number {
  display: grid;
  place-items: center;
  align-self: center;
  width: 22px;
  height: 22px;
  border-radius: 999px;
  color: #ffffff;
  background: var(--accent);
  font-size: 0.78rem;
  font-weight: 900;
  line-height: 1;
}

.info-step p {
  margin: 0;
  color: var(--ink);
  font-size: 0.92rem;
  font-weight: 650;
  line-height: 1.38;
}

.info-thumbnails {
  display: flex;
  gap: 6px;
  align-items: start;
  min-width: 0;
}

.info-thumbnail {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border-radius: 8px;
  color: #ffffff;
  background: var(--accent);
}

.info-piece-thumbnail {
  color: var(--ink);
  background: #ffffff;
  border: 1px solid var(--line);
}

.info-piece-thumbnail img {
  width: 31px;
  height: 31px;
}

.info-remove-thumbnail {
  color: var(--muted);
  background: #ffffff;
  border: 1px solid var(--line);
}

.info-toggle-thumbnails {
  display: grid;
  gap: 5px;
  justify-items: start;
  justify-self: start;
}

.info-state-chip {
  display: grid;
  place-items: center;
  width: max-content;
  min-height: 28px;
  padding: 0 8px;
  border-radius: 999px;
  font-size: 0.68rem;
  font-weight: 850;
  white-space: nowrap;
}

.info-orientation-chip {
  background: rgba(226, 165, 45, 0.14);
  color: #825600;
}

.info-turn-chip {
  background: rgba(15, 143, 123, 0.12);
  color: var(--accent-dark);
}

.file-input {
  position: fixed;
  left: -9999px;
  top: 0;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

.status-line {
  margin-top: 2px;
  color: var(--muted);
  font-size: 0.84rem;
  font-weight: 650;
  overflow-wrap: anywhere;
}

.board-state-row {
  width: min(100%, 620px, calc(100vh - 230px));
  margin: -2px auto 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.state-toggle {
  min-height: 38px;
  padding: 0 12px;
  border: 1px solid transparent;
  border-radius: 999px;
  font-size: 0.82rem;
  font-weight: 850;
  white-space: nowrap;
  cursor: pointer;
}

.orientation-toggle {
  background: rgba(226, 165, 45, 0.14);
  color: #825600;
}

.turn-toggle {
  margin-left: auto;
  background: rgba(15, 143, 123, 0.12);
  color: var(--accent-dark);
}

.board-stage {
  position: relative;
  width: min(100%, 620px, calc(100vh - 230px));
  margin: 0 auto;
}

.board-grid {
  display: grid;
  grid-template-columns: repeat(8, minmax(0, 1fr));
  width: 100%;
  min-width: 0;
  aspect-ratio: 1;
  margin: 0;
  border: 2px solid #252a31;
  border-radius: 8px;
  overflow: hidden;
  background: #252a31;
}

.loading-overlay {
  position: absolute;
  inset: 2px;
  z-index: 3;
  display: grid;
  place-items: center;
  align-content: center;
  gap: 12px;
  border-radius: 6px;
  background: rgba(251, 252, 254, 0.88);
  color: var(--ink);
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
}

.loading-overlay[hidden] {
  display: none;
}

.piece-loader {
  position: relative;
  width: 132px;
  height: 58px;
}

.loader-piece {
  position: absolute;
  bottom: 0;
  left: calc(var(--slot) * 22px);
  width: 38px;
  height: 38px;
  object-fit: contain;
  filter: drop-shadow(0 2px 2px rgba(32, 36, 45, 0.22));
  animation: piece-hop 1.15s ease-in-out infinite;
  animation-delay: calc(var(--slot) * 0.12s);
}

.loader-copy {
  color: var(--accent-dark);
  font-size: 0.86rem;
  font-weight: 900;
}

@keyframes piece-hop {
  0%,
  72%,
  100% {
    transform: translateY(0) scale(0.96);
    opacity: 0.66;
  }

  28% {
    transform: translateY(-14px) scale(1.08);
    opacity: 1;
  }
}

@media (prefers-reduced-motion: reduce) {
  .loader-piece {
    animation: none;
  }
}

.square {
  position: relative;
  display: grid;
  place-items: center;
  width: 100%;
  aspect-ratio: 1;
  padding: 0;
  border: 0;
  border-radius: 0;
  line-height: 1;
  box-shadow: none;
  cursor: pointer;
}

.square.light {
  background: var(--light-square);
}

.square.dark {
  background: var(--dark-square);
}

.board-coordinate {
  position: absolute;
  z-index: 2;
  font-size: 0.82rem;
  font-weight: 900;
  line-height: 1;
  pointer-events: none;
  user-select: none;
}

.square.light .board-coordinate {
  color: var(--dark-square);
}

.square.dark .board-coordinate {
  color: var(--light-square);
}

.rank-coordinate {
  top: 5px;
  left: 5px;
}

.file-coordinate {
  right: 5px;
  bottom: 5px;
}

.square.low-confidence::after {
  content: "";
  position: absolute;
  z-index: 3;
  right: 5px;
  top: 5px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--danger);
}

.piece-image {
  display: block;
  width: 92%;
  height: 92%;
  object-fit: contain;
  filter: drop-shadow(0 1px 1px rgba(0, 0, 0, 0.28));
  pointer-events: none;
  user-select: none;
}

.edit-panel {
  width: min(100%, 620px, calc(100vh - 230px));
  margin: 12px auto 0;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfcfe;
  display: grid;
  gap: 10px;
}

.editor-row,
.control-group {
  display: grid;
  gap: 7px;
}

.control-label {
  color: var(--muted);
  font-size: 0.74rem;
  font-weight: 850;
  line-height: 1;
  text-transform: uppercase;
}

.piece-editor {
  --piece-tile-size: 44px;
  display: grid;
  gap: 8px;
}

.piece-side-row {
  display: grid;
  grid-template-columns: minmax(0, 6fr) minmax(88px, 1.34fr);
  gap: 8px;
  align-items: start;
}

.piece-row {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 6px;
}

.piece-button {
  display: grid;
  place-items: center;
  min-width: 0;
  aspect-ratio: 1;
  padding: 2px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  color: var(--ink);
  cursor: pointer;
}

.piece-button.empty-piece {
  aspect-ratio: 1;
  padding: 2px;
  color: var(--muted);
}

.empty-piece-slot {
  display: grid;
  align-self: stretch;
}

.empty-piece-slot .piece-button {
  width: 100%;
  height: auto;
  min-height: 0;
  aspect-ratio: 1;
}

.empty-icon {
  width: 68%;
  height: 68%;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.4;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.piece-button.is-selected,
.toggle-button.is-active {
  border-color: var(--accent);
  background: rgba(15, 143, 123, 0.12);
  box-shadow: inset 0 0 0 1px rgba(15, 143, 123, 0.18);
}

.toggle-button:disabled,
.toggle-button.is-disabled {
  border-color: #d8dde6;
  background: #eef1f5;
  color: #8c94a3;
  box-shadow: none;
  cursor: not-allowed;
}

.castle-controls {
  display: grid;
  height: var(--piece-tile-size);
  min-width: 0;
}

.castle-color-group {
  display: grid;
  align-content: stretch;
  height: 100%;
  min-width: 0;
}

.castle-button-row {
  display: grid;
  grid-template-rows: repeat(2, minmax(0, 1fr));
  gap: 6px;
  height: 100%;
}

.toggle-button {
  display: grid;
  place-items: center;
  min-height: 38px;
  padding: 0 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  color: var(--ink);
  font-size: 0.82rem;
  font-weight: 850;
  line-height: 1.05;
  cursor: pointer;
}

.castle-button {
  min-height: 0;
  padding: 0 5px;
  font-size: 0.78rem;
  letter-spacing: 0;
}

.piece-utility-row {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr)) minmax(88px, 1.34fr);
  gap: 6px;
  align-items: start;
}

.remove-control {
  display: grid;
  gap: 6px;
  grid-column: 1;
  width: 100%;
  min-width: 0;
  justify-items: stretch;
}

.remove-control .control-label {
  width: 100%;
  text-align: left;
}

.en-passant-control {
  display: grid;
  grid-column: 2 / -1;
  gap: 6px;
}

.en-passant-select {
  width: 100%;
  min-height: 42px;
  padding: 0 34px 0 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  color: var(--ink);
  font-weight: 750;
}

.analysis-actions {
  width: min(100%, 620px, calc(100vh - 230px));
  margin: 12px auto 0;
}

.analysis-link {
  display: grid;
  place-items: center;
  min-height: 50px;
  width: 100%;
  border: 1px solid rgba(15, 143, 123, 0.45);
  border-radius: 8px;
  color: #fff;
  background: var(--accent);
  font-weight: 900;
  text-decoration: none;
}

.analysis-link.disabled {
  pointer-events: none;
  color: var(--muted);
  border-color: var(--line);
  background: #eef1f5;
}

.fen-output {
  display: block;
  width: min(100%, 620px, calc(100vh - 230px));
  min-height: 42px;
  margin: 10px auto 0;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--ink);
  background: #fbfcfe;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
  font-size: 0.86rem;
  line-height: 1.35;
  overflow-wrap: anywhere;
  white-space: normal;
  -webkit-user-select: text;
  user-select: text;
}

@media (max-width: 560px) {
  .app-shell {
    gap: 10px;
  }

  .topbar {
    grid-template-columns: minmax(0, 1fr);
    align-items: start;
  }

  .title-block {
    min-height: 0;
  }

  .source-actions {
    justify-content: flex-end;
    gap: 6px;
    width: 100%;
  }

  .info-card {
    padding: 14px;
  }

  .info-step {
    grid-template-columns: 22px minmax(72px, auto);
    column-gap: 12px;
    row-gap: 8px;
  }

  .info-step p {
    grid-column: 2 / -1;
  }

  .info-thumbnails {
    min-height: 40px;
  }

  .brand-logo {
    width: min(343px, 100%);
    margin-bottom: 8px;
    gap: 8px;
  }

  .brand-mark {
    width: 76px;
    height: auto;
  }

  .brand-name {
    font-size: clamp(0.9rem, 4vw, 0.96rem);
  }

  .coffee-link {
    width: 155px;
    height: 33px;
    margin-right: auto;
  }

  .board-stage,
  .board-state-row,
  .edit-panel,
  .analysis-actions,
  .fen-output {
    width: 100%;
  }

  .piece-side-row {
    grid-template-columns: minmax(0, 6fr) minmax(88px, 1.34fr);
  }

  .piece-utility-row {
    grid-template-columns: repeat(6, minmax(0, 1fr)) minmax(88px, 1.34fr);
  }

  .remove-control .control-label {
    font-size: 0.6rem;
    text-align: left;
  }

  .en-passant-control {
    margin-left: 12px;
  }
}

@media (max-width: 380px) {
  .brand-logo {
    width: min(312px, 100%);
  }

  .brand-mark {
    width: 68px;
    height: auto;
  }

  .brand-name {
    font-size: 0.84rem;
  }

  .tagline {
    font-size: 0.84rem;
  }

  .info-action {
    width: 20px;
    height: 20px;
  }

  .info-action span {
    font-size: 16px;
  }

  .coffee-link {
    width: 136px;
    height: 29px;
  }

  .icon-action {
    width: 42px;
    height: 42px;
  }
}
