:root {
  --paper: #f4f0e7;
  --paper-deep: #eae4d8;
  --ink: #1d2a25;
  --muted: #6d756f;
  --line: #ded8cc;
  --panel: rgba(255, 255, 255, 0.84);
  --forest: #265f4c;
  --forest-dark: #174638;
  --green-soft: #e8f2ec;
  --orange: #f08a45;
  --danger: #b6453a;
  --shadow: 0 18px 50px rgba(51, 53, 44, 0.09);
  --red: #e94e45;
  --yellow: #f3c742;
  --blue: #3c82d4;
  --green: #52a55a;
  --white-piece: #fffef8;
}

* { box-sizing: border-box; }

html, body { min-height: 100%; }

body {
  margin: 0;
  color: var(--ink);
  background:
    radial-gradient(circle at 2% 4%, rgba(249, 193, 89, 0.22), transparent 22rem),
    radial-gradient(circle at 95% 98%, rgba(52, 130, 104, 0.14), transparent 28rem),
    var(--paper);
  font-family: Inter, "PingFang SC", "Microsoft YaHei", system-ui, -apple-system, sans-serif;
  -webkit-font-smoothing: antialiased;
}

button, input { font: inherit; }
button { color: inherit; }

.app-shell {
  width: min(1600px, calc(100% - 48px));
  margin: 0 auto;
  padding: 24px 0 36px;
}

.topbar {
  min-height: 76px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  margin-bottom: 20px;
}

.brand-lockup { display: flex; align-items: center; gap: 14px; }
.brand-lockup h1 { margin: 2px 0 0; font-size: clamp(22px, 2vw, 31px); letter-spacing: -0.03em; }
.eyebrow, .section-kicker { margin: 0; color: var(--forest); font-size: 12px; font-weight: 800; letter-spacing: .14em; text-transform: uppercase; }

.brand-mark {
  width: 52px;
  height: 52px;
  padding: 7px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 3px;
  border-radius: 16px;
  background: var(--ink);
  box-shadow: 0 8px 18px rgba(29, 42, 37, .18);
  transform: rotate(-2deg);
}
.brand-mark span { border-radius: 4px; }
.brand-mark span:nth-child(1) { background: var(--red); }
.brand-mark span:nth-child(2) { background: var(--yellow); }
.brand-mark span:nth-child(3) { background: var(--blue); }
.brand-mark span:nth-child(4) { background: var(--green); }

.header-actions { display: flex; align-items: center; gap: 10px; }
.design-name {
  height: 46px;
  min-width: 200px;
  padding: 5px 13px;
  display: grid;
  gap: 1px;
  background: rgba(255,255,255,.65);
  border: 1px solid var(--line);
  border-radius: 12px;
}
.design-name span { color: var(--muted); font-size: 10px; font-weight: 700; }
.design-name input { width: 100%; padding: 0; border: 0; outline: 0; color: var(--ink); background: transparent; font-size: 14px; font-weight: 750; }

.button, .tool-button {
  border: 0;
  cursor: pointer;
  transition: transform .18s ease, box-shadow .18s ease, background .18s ease, opacity .18s ease;
}
.button:active, .tool-button:active { transform: translateY(1px); }
.button:focus-visible, .tool-button:focus-visible, .piece-card:focus-visible, .cell:focus-visible { outline: 3px solid rgba(39, 105, 84, .28); outline-offset: 2px; }
.button {
  min-height: 46px;
  padding: 0 16px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border-radius: 12px;
  font-size: 14px;
  font-weight: 800;
}
.button svg, .tool-button svg, .save-state svg { width: 19px; height: 19px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.button-primary { color: white; background: var(--forest); box-shadow: 0 8px 18px rgba(38, 95, 76, .22); }
.button-primary:hover { background: var(--forest-dark); transform: translateY(-1px); }
.button-ghost { background: rgba(255,255,255,.78); border: 1px solid var(--line); }
.button-ghost:hover { background: white; }
.button-danger { color: white; background: var(--danger); }

.workspace {
  display: grid;
  grid-template-columns: 286px minmax(530px, 1fr) 286px;
  gap: 16px;
  align-items: stretch;
}

.panel {
  min-width: 0;
  background: var(--panel);
  border: 1px solid rgba(213, 207, 195, .9);
  border-radius: 22px;
  box-shadow: var(--shadow);
  backdrop-filter: blur(16px);
}

.palette-panel, .stats-panel { padding: 22px; }
.panel-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.panel-heading h2 { margin: 5px 0 0; font-size: 19px; letter-spacing: -.02em; }
.inventory-total { padding: 6px 9px; color: var(--forest); background: var(--green-soft); border-radius: 20px; font-size: 11px; font-weight: 800; white-space: nowrap; }
.panel-note { margin: 9px 0 20px; color: var(--muted); font-size: 12px; line-height: 1.55; }

.palette-section + .palette-section { margin-top: 21px; }
.palette-section h3 { margin: 0 0 10px; font-size: 13px; }
.palette-section h3 span { color: var(--muted); font-size: 10px; font-weight: 500; float: right; }
.piece-grid { display: grid; gap: 8px; }
.solid-grid { grid-template-columns: repeat(4, 1fr); }
.bicolor-grid { grid-template-columns: repeat(2, 1fr); }

.piece-card {
  min-width: 0;
  padding: 8px;
  display: flex;
  align-items: center;
  gap: 8px;
  border: 1px solid var(--line);
  border-radius: 11px;
  background: rgba(255,255,255,.68);
  cursor: pointer;
  position: relative;
  transition: border-color .18s ease, box-shadow .18s ease, transform .18s ease;
  touch-action: none;
}
.piece-card:hover { transform: translateY(-1px); border-color: #b9c8c0; }
.piece-card.is-selected { border-color: var(--forest); box-shadow: 0 0 0 2px rgba(38, 95, 76, .12); background: #fff; }
.piece-card.is-empty { opacity: .48; }
.solid-grid .piece-card { justify-content: center; padding: 8px 4px; }
.piece-preview, .usage-swatch, .status-swatch {
  --c1: var(--red);
  --c2: var(--red);
  --rotation: 0deg;
  flex: 0 0 auto;
  display: block;
  border: 1px solid rgba(34, 38, 35, .2);
  background: linear-gradient(calc(135deg + var(--rotation)), var(--c1) 0 49.6%, var(--c2) 50.4% 100%);
}
.piece-preview { width: 30px; height: 30px; border-radius: 7px; box-shadow: inset 0 0 0 1px rgba(255,255,255,.22); }
.solid-grid .piece-preview { width: 34px; height: 34px; }
.piece-copy { min-width: 0; display: grid; gap: 2px; text-align: left; }
.piece-copy strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 11px; }
.piece-copy span { color: var(--muted); font-size: 10px; }
.solid-grid .piece-copy { display: none; }

.palette-tip { margin-top: 18px; padding: 11px; display: flex; align-items: center; gap: 10px; color: #4d5a54; background: #f4f1e9; border-radius: 12px; }
.tip-icon { width: 30px; height: 30px; display: grid; place-items: center; border-radius: 50%; color: white; background: var(--forest); font-size: 17px; }
.palette-tip p { margin: 0; font-size: 10px; line-height: 1.5; }
.palette-tip strong { color: var(--ink); font-size: 11px; }

.board-panel { padding: 18px 20px 15px; display: flex; flex-direction: column; }
.board-toolbar { min-height: 42px; display: flex; justify-content: space-between; gap: 12px; }
.tool-group { display: flex; gap: 6px; }
.tool-button {
  min-height: 38px;
  padding: 0 11px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: #52605a;
  background: #f0eee8;
  border-radius: 10px;
  font-size: 12px;
  font-weight: 750;
}
.tool-button svg { width: 17px; height: 17px; }
.tool-button:hover:not(:disabled), .tool-button.is-active { color: var(--forest); background: #e3eee7; }
.tool-button:disabled { opacity: .38; cursor: not-allowed; }
.danger-text { color: var(--danger); }

.board-wrap {
  width: min(100%, 690px);
  margin: 18px auto 12px;
  padding: 0 0 0 25px;
  display: grid;
  grid-template-columns: 25px minmax(0, 1fr);
  grid-template-rows: 25px minmax(0, 1fr);
}
.axis-corner { grid-column: 1; grid-row: 1; display: grid; place-items: center; color: #95a099; font-size: 12px; }
.axis { color: #5d6862; font-size: 10px; font-weight: 800; }
.axis-top { grid-column: 2; grid-row: 1; display: grid; grid-template-columns: repeat(10, 1fr); align-items: center; text-align: center; }
.axis-left { grid-column: 1; grid-row: 2; display: grid; grid-template-rows: repeat(10, 1fr); align-items: center; text-align: center; }

.board {
  grid-column: 2;
  grid-row: 2;
  aspect-ratio: 1;
  display: grid;
  grid-template-columns: repeat(10, 1fr);
  grid-template-rows: repeat(10, 1fr);
  border: 5px solid #d8d4cb;
  border-radius: 10px;
  overflow: hidden;
  background: #fffefa;
  box-shadow: 0 18px 35px rgba(38, 43, 38, .12), inset 0 0 0 1px #bebcb4;
  touch-action: none;
  user-select: none;
}

.cell {
  min-width: 0;
  min-height: 0;
  padding: 0;
  position: relative;
  border: 0;
  border-right: 1px solid #d9d8d3;
  border-bottom: 1px solid #d9d8d3;
  background: #fffef9;
  cursor: crosshair;
}
.cell:nth-child(10n) { border-right: 0; }
.cell:nth-last-child(-n + 10) { border-bottom: 0; }
.cell::after { content: ""; position: absolute; inset: 0; pointer-events: none; transition: box-shadow .12s ease; }
.cell:hover::after { box-shadow: inset 0 0 0 2px rgba(38, 95, 76, .35); }
.cell.is-active::after { box-shadow: inset 0 0 0 3px var(--forest), inset 0 0 0 5px rgba(255,255,255,.7); z-index: 2; }
.cell .block-face { position: absolute; inset: 2px; border-radius: 3px; background: linear-gradient(calc(135deg + var(--rotation)), var(--c1) 0 49.6%, var(--c2) 50.4% 100%); box-shadow: inset 0 0 0 1px rgba(0,0,0,.14), inset 0 0 0 2px rgba(255,255,255,.2); pointer-events: none; }
.cell.just-placed .block-face { animation: pop .18s ease-out; }
@keyframes pop { from { transform: scale(.72); opacity: .5; } to { transform: scale(1); opacity: 1; } }

.board-status { min-height: 58px; padding: 10px 12px 0; display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 12px; border-top: 1px solid #e7e2d9; }
.selected-status { display: flex; align-items: center; gap: 9px; }
.status-swatch { width: 29px; height: 29px; border-radius: 7px; }
.selected-status div { display: grid; gap: 1px; }
.muted-label { color: var(--muted); font-size: 9px; }
.selected-status strong { font-size: 12px; }
.selection-location { color: var(--forest); font-size: 11px; font-weight: 800; text-align: center; }
.shortcut-hint { justify-self: end; color: #8b918d; font-size: 9px; }

.stats-heading { align-items: center; }
.usage-ring {
  --progress: 0;
  width: 66px;
  aspect-ratio: 1;
  padding: 6px;
  border-radius: 50%;
  background: conic-gradient(var(--forest) calc(var(--progress) * 1%), #e8e4db 0);
}
.usage-ring > div { width: 100%; height: 100%; display: grid; place-content: center; text-align: center; background: white; border-radius: 50%; }
.usage-ring strong { font-size: 18px; line-height: 1; }
.usage-ring span { color: var(--muted); font-size: 8px; }

.status-card { margin: 20px 0 12px; padding: 12px; display: flex; align-items: center; gap: 10px; border-radius: 12px; }
.status-card.is-ok { background: var(--green-soft); }
.status-card.is-warning { background: #fff0e9; }
.status-dot { width: 10px; height: 10px; border-radius: 50%; background: #3f9a65; box-shadow: 0 0 0 5px rgba(63,154,101,.12); }
.is-warning .status-dot { background: var(--orange); box-shadow: 0 0 0 5px rgba(240,138,69,.14); }
.status-card strong { font-size: 12px; }
.status-card p { margin: 2px 0 0; color: var(--muted); font-size: 9px; }

.summary-strip { padding: 13px 4px; display: grid; grid-template-columns: repeat(3, 1fr); background: #f5f2eb; border-radius: 12px; }
.summary-strip div { display: grid; gap: 3px; text-align: center; border-right: 1px solid #ded9cf; }
.summary-strip div:last-child { border-right: 0; }
.summary-strip span { color: var(--muted); font-size: 9px; }
.summary-strip strong { font-size: 15px; }

.usage-list-wrap { margin-top: 18px; }
.usage-list-heading { margin-bottom: 7px; display: flex; justify-content: space-between; color: var(--muted); font-size: 9px; font-weight: 700; }
.usage-list { max-height: 435px; padding-right: 5px; overflow: auto; scrollbar-width: thin; }
.usage-row { min-height: 36px; display: grid; grid-template-columns: 23px minmax(0, 1fr) auto; gap: 8px; align-items: center; border-bottom: 1px solid #ece8e0; }
.usage-swatch { width: 23px; height: 23px; border-radius: 5px; }
.usage-name { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 10px; font-weight: 700; }
.usage-count { min-width: 40px; color: var(--muted); font-size: 10px; text-align: right; }
.usage-count strong { color: var(--ink); }
.usage-count.near-limit strong { color: var(--orange); }
.usage-count.at-limit strong { color: var(--danger); }

.save-state { margin-top: 16px; padding: 10px; display: flex; align-items: center; gap: 8px; color: var(--muted); background: #f7f5ef; border-radius: 10px; font-size: 9px; }
.save-state svg { width: 17px; color: var(--forest); }
.save-state.is-saved { color: var(--forest); background: var(--green-soft); }

.toast-region { position: fixed; right: 24px; bottom: 24px; z-index: 20; display: grid; gap: 8px; }
.toast { min-width: 220px; padding: 12px 15px; color: white; background: #1d2a25; border-radius: 12px; box-shadow: 0 14px 32px rgba(0,0,0,.2); font-size: 12px; font-weight: 700; animation: toast-in .22s ease-out; }
.toast.is-error { background: var(--danger); }
@keyframes toast-in { from { transform: translateY(8px); opacity: 0; } }

.confirm-dialog { width: min(390px, calc(100% - 32px)); padding: 0; border: 0; border-radius: 20px; box-shadow: 0 28px 80px rgba(0,0,0,.24); }
.confirm-dialog::backdrop { background: rgba(18, 29, 24, .42); backdrop-filter: blur(3px); }
.confirm-dialog form { padding: 28px; text-align: center; }
.dialog-icon { width: 46px; height: 46px; margin: 0 auto 12px; display: grid; place-items: center; color: var(--danger); background: #f9e6e3; border-radius: 50%; font-weight: 900; }
.confirm-dialog h2 { margin: 0; font-size: 19px; }
.confirm-dialog p { margin: 8px 0 22px; color: var(--muted); font-size: 12px; }
.dialog-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 9px; }

@media (max-width: 1180px) {
  .workspace { grid-template-columns: 245px minmax(510px, 1fr); }
  .stats-panel { grid-column: 1 / -1; display: grid; grid-template-columns: 1.1fr 1fr 1.2fr; gap: 16px; align-items: start; }
  .stats-panel > .panel-heading { grid-column: 1; }
  .stats-panel > .status-card, .stats-panel > .summary-strip { grid-column: 1; }
  .usage-list-wrap { grid-column: 2 / 4; grid-row: 1 / 4; margin: 0; }
  .usage-list { max-height: 190px; display: grid; grid-template-columns: repeat(2, 1fr); column-gap: 14px; }
  .save-state { display: none; }
}

@media (max-width: 860px) {
  .app-shell { width: min(100% - 24px, 720px); padding-top: 14px; }
  .topbar { align-items: flex-start; }
  .header-actions { flex-wrap: wrap; justify-content: flex-end; }
  .design-name { order: 3; width: 100%; }
  .workspace { grid-template-columns: 1fr; }
  .board-panel { grid-row: 1; }
  .palette-panel { grid-row: 2; }
  .palette-panel, .stats-panel { padding: 18px; }
  .palette-panel { display: grid; grid-template-columns: 1fr 1.3fr; gap: 14px; }
  .palette-panel > .panel-heading, .palette-panel > .panel-note, .palette-panel > .palette-tip { grid-column: 1 / -1; }
  .palette-section + .palette-section { margin-top: 0; }
  .stats-panel { grid-column: auto; }
}

@media (max-width: 620px) {
  .app-shell { width: calc(100% - 16px); padding-bottom: 16px; }
  .topbar { display: grid; gap: 12px; }
  .brand-mark { width: 44px; height: 44px; border-radius: 13px; }
  .brand-lockup h1 { font-size: 21px; }
  .header-actions { display: grid; grid-template-columns: 1fr 1.2fr; width: 100%; }
  .design-name { grid-column: 1 / -1; order: 0; }
  .button { min-height: 43px; padding: 0 10px; font-size: 12px; }
  .board-panel { padding: 10px 8px 8px; border-radius: 16px; }
  .board-toolbar { flex-wrap: wrap; }
  .tool-group { flex: 1; }
  .tool-button { flex: 1; justify-content: center; padding: 0 7px; font-size: 10px; }
  .utility-tools { width: 100%; }
  .board-wrap { margin-top: 10px; padding-left: 19px; grid-template-columns: 19px minmax(0, 1fr); grid-template-rows: 20px minmax(0, 1fr); }
  .axis { font-size: 8px; }
  .board { border-width: 4px; }
  .board-status { grid-template-columns: 1fr auto; }
  .shortcut-hint { display: none; }
  .selection-location { text-align: right; }
  .palette-panel { grid-template-columns: 1fr; }
  .palette-panel > * { grid-column: auto !important; }
  .solid-grid { grid-template-columns: repeat(4, 1fr); }
  .bicolor-grid { grid-template-columns: repeat(2, 1fr); }
  .stats-panel { display: block; }
  .usage-list-wrap { margin-top: 18px; }
  .usage-list { max-height: 310px; display: block; }
  .save-state { display: flex; }
  .toast-region { left: 12px; right: 12px; bottom: 12px; }
  .toast { min-width: 0; text-align: center; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
