:root {
  color-scheme: light;
  --ink: #172033;
  --text: #263449;
  --muted: #68758a;
  --line: #d8e0eb;
  --line-strong: #c1ccda;
  --surface: #ffffff;
  --surface-soft: #f4f7fb;
  --surface-blue: #eaf1fb;
  --canvas: #1d2633;
  --empty-cell: #2b3646;
  --blue: #2563eb;
  --blue-dark: #1d4ed8;
  --green: #087a52;
  --green-soft: #ddf7e9;
  --amber: #9a5200;
  --amber-soft: #fff2d8;
  --red: #b42318;
  --red-soft: #fee8e7;
  --shadow: 0 12px 28px rgba(24, 39, 64, 0.09);
  --radius: 14px;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

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

body {
  overflow: hidden;
  color: var(--text);
  background:
    radial-gradient(circle at 8% 0%, rgba(79, 134, 214, 0.12), transparent 28rem),
    #e9eff7;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 16px;
}

button, input { font: inherit; }
button, .button { -webkit-tap-highlight-color: transparent; }

.app-shell { height: 100vh; display: grid; grid-template-rows: auto minmax(0, 1fr); }

.topbar {
  min-height: 72px;
  display: grid;
  grid-template-columns: auto minmax(520px, 1fr) minmax(220px, auto);
  align-items: center;
  gap: 22px;
  padding: 12px 18px;
  color: white;
  background: linear-gradient(115deg, #111a2b 0%, #1d2b45 62%, #203453 100%);
  box-shadow: 0 8px 24px rgba(19, 31, 52, 0.2);
  z-index: 10;
}

.brand { display: flex; align-items: center; gap: 11px; min-width: 165px; }
.brand > span:last-child { display: grid; line-height: 1.05; }
.brand strong { font-size: 1.05rem; letter-spacing: 0.01em; }
.brand small { margin-top: 4px; color: #a9bad3; font-size: 0.78rem; letter-spacing: 0.08em; text-transform: uppercase; }
.brand-mark { width: 34px; height: 34px; display: grid; grid-template-columns: 1fr 1fr; gap: 3px; padding: 4px; border-radius: 9px; background: rgba(255,255,255,.08); }
.brand-mark i { display: block; border-radius: 2px; background: #60a5fa; }
.brand-mark i:last-child { background: #f59e0b; }

.top-actions { display: flex; align-items: center; gap: 8px; min-width: 0; }
.session-summary { min-width: 0; display: grid; justify-items: end; gap: 3px; }
.session-summary span { max-width: 310px; overflow: hidden; color: #b8c7db; font-size: 0.82rem; text-overflow: ellipsis; white-space: nowrap; }
.session-summary strong { color: #d7f9e6; font-size: 0.9rem; }

.button, .mini-button, .icon-button {
  border: 1px solid var(--line-strong);
  border-radius: 9px;
  color: var(--text);
  background: var(--surface);
  cursor: pointer;
  font-weight: 700;
  transition: transform .15s ease, background .15s ease, border-color .15s ease, opacity .15s ease;
}
.button { display: inline-flex; min-height: 40px; align-items: center; justify-content: center; padding: 8px 14px; text-decoration: none; white-space: nowrap; }
.topbar .button { color: #e7eef8; border-color: rgba(255,255,255,.18); background: rgba(255,255,255,.08); }
.button:hover:not(:disabled), .mini-button:hover:not(:disabled), .icon-button:hover:not(:disabled) { transform: translateY(-1px); border-color: #97acd0; background: #edf4ff; }
.topbar .button:hover:not(:disabled) { background: rgba(255,255,255,.16); }
.button.primary { color: white; border-color: #3b82f6; background: var(--blue); }
.button.primary:hover:not(:disabled) { background: var(--blue-dark); }
.button.success { color: #d9ffeb; border-color: rgba(74, 222, 128, .35); background: rgba(9, 130, 86, .45); }
.button.large { min-height: 48px; padding-inline: 22px; }
.button:disabled, .mini-button:disabled, .icon-button:disabled { cursor: not-allowed; opacity: .42; transform: none; }

.workspace { min-height: 0; display: grid; grid-template-columns: 238px minmax(420px, 1fr) 330px; gap: 12px; padding: 12px; }
.panel { min-width: 0; min-height: 0; overflow: hidden; border: 1px solid rgba(186, 198, 215, .82); border-radius: var(--radius); background: rgba(255,255,255,.92); box-shadow: var(--shadow); }

.panel-heading { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 15px 15px 12px; border-bottom: 1px solid var(--line); }
.panel-heading.compact { padding: 0 0 12px; border: 0; }
.eyebrow { display: block; margin-bottom: 2px; color: var(--blue); font-size: .72rem; font-weight: 800; letter-spacing: .11em; text-transform: uppercase; }
h1, h2, h3, p { margin-top: 0; }
h2 { margin-bottom: 0; color: var(--ink); font-size: 1.2rem; line-height: 1.15; }
h3 { margin-bottom: 0; color: var(--ink); font-size: 1rem; }
.muted { color: var(--muted); font-size: .83rem; }

.levels-panel { display: grid; grid-template-rows: auto auto minmax(0, 1fr); }
.selection-bar { padding: 8px 9px 0; }
.selection-bar .button { width: 100%; min-height: 34px; padding: 6px 10px; color: #174278; border-color: #a9c4ec; background: #edf4ff; font-size: .8rem; }
.selection-bar .button:hover:not(:disabled) { border-color: var(--blue); background: #dfeaff; }
.selection-hint { display: block; margin-top: 5px; color: var(--muted); font-size: .7rem; text-align: center; }
.level-gallery { min-height: 0; overflow: auto; padding: 9px; scrollbar-color: #aebbd0 transparent; scrollbar-width: thin; }
.gallery-empty { min-height: 100%; display: grid; place-content: center; justify-items: center; padding: 28px 12px; color: var(--muted); text-align: center; }
.gallery-empty p { max-width: 180px; line-height: 1.5; }
.gallery-empty code { font-size: .78rem; }
.empty-grid { width: 54px; height: 54px; margin-bottom: 12px; border: 4px solid #8da7cc; border-radius: 12px; background: conic-gradient(from 90deg at 2px 2px, #d9e5f5 90deg, transparent 0) 0 0 / 12px 12px; }

.level-card-wrap { position: relative; margin-bottom: 7px; content-visibility: auto; contain: layout paint; contain-intrinsic-size: auto 90px; }
.level-card { width: 100%; display: grid; grid-template-columns: 72px minmax(0, 1fr); align-items: center; gap: 10px; padding: 7px; border: 2px solid transparent; border-radius: 11px; color: var(--text); background: #f7f9fc; cursor: pointer; text-align: left; transition: border .14s ease, background .14s ease, transform .14s ease; }
.level-card:hover { transform: translateY(-1px); border-color: #b8c8df; background: white; }
.level-card.selected { border-color: var(--blue); background: #e8f0ff; box-shadow: 0 0 0 2px rgba(37,99,235,.08); }
.level-card.saved:not(.selected) { border-color: #83d7ae; background: #edfbf4; }
.level-card.dirty .level-card-state { color: var(--amber); }
.level-card-wrap.checked .level-card { border-color: #6d9fec; box-shadow: 0 0 0 2px rgba(37,99,235,.09); }
.level-check { position: absolute; top: 11px; left: 11px; z-index: 2; display: grid; place-items: center; width: 24px; height: 24px; border: 1px solid #aab8ca; border-radius: 7px; background: rgba(255,255,255,.94); box-shadow: 0 2px 7px rgba(15,36,68,.16); cursor: pointer; }
.level-check input { width: 16px; height: 16px; margin: 0; accent-color: var(--blue); cursor: pointer; }
.thumb-canvas { width: 72px; height: 72px; border-radius: 7px; background: var(--canvas); image-rendering: pixelated; }
.level-card-copy { min-width: 0; display: grid; gap: 3px; }
.level-card-copy strong { color: var(--ink); font-size: .91rem; }
.level-card-copy span { overflow: hidden; color: var(--muted); font-size: .75rem; text-overflow: ellipsis; white-space: nowrap; }
.level-card-state { color: var(--green) !important; font-weight: 800; }

.editor-panel { display: grid; grid-template-rows: auto minmax(0, 1fr) auto; background: #dfe7f1; }
.editor-toolbar { display: flex; min-height: 66px; align-items: center; justify-content: space-between; gap: 16px; padding: 10px 14px; border-bottom: 1px solid #c9d3e0; background: rgba(255,255,255,.96); }
.level-nav, .view-tools { display: flex; align-items: center; gap: 8px; }
.level-nav-copy { display: grid; min-width: 130px; text-align: center; }
.level-nav strong { color: var(--ink); font-size: .95rem; }
.level-nav span { color: var(--muted); font-size: .76rem; }
.level-stepper { display: grid; grid-template-rows: repeat(2, 26px); gap: 4px; }
.level-stepper .icon-button { width: 36px; height: 26px; font-size: 1rem; line-height: 1; }
.icon-button { width: 36px; height: 36px; padding: 0; font-size: 1.2rem; }
.mini-button { min-height: 36px; padding: 6px 10px; font-size: .82rem; }
.toggle { display: flex; align-items: center; gap: 5px; margin-left: 4px; color: var(--muted); font-size: .82rem; font-weight: 700; }
.toggle input { accent-color: var(--blue); }
.background-control { display: flex; align-items: center; gap: 6px; margin-left: 3px; color: var(--muted); font-size: .82rem; font-weight: 700; }
.background-control input { width: 31px; height: 31px; padding: 2px; border: 1px solid var(--line-strong); border-radius: 8px; background: white; cursor: pointer; }

.canvas-viewport { position: relative; min-height: 0; overflow: auto; display: grid; align-items: start; justify-items: start; padding: 24px; background-color: var(--canvas); background-image: radial-gradient(circle at 1px 1px, rgba(255,255,255,.055) 1px, transparent 0); background-size: 18px 18px; scrollbar-color: #63738a #263140; }
#pixel-canvas { display: block; margin: auto; box-shadow: 0 16px 38px rgba(0,0,0,.28); image-rendering: pixelated; touch-action: none; cursor: crosshair; }
#pixel-canvas.selection-can-move { cursor: grab; }
#pixel-canvas.moving-region { cursor: grabbing; }
.editor-empty { position: absolute; inset: 0; display: grid; place-content: center; justify-items: center; padding: 24px; color: #c0ccdd; text-align: center; }
.editor-empty h1 { max-width: 560px; margin-bottom: 9px; color: white; font-size: clamp(1.4rem, 3vw, 2.15rem); line-height: 1.18; }
.editor-empty p { margin-bottom: 22px; color: #a9b6c9; }
.empty-art { width: 76px; height: 76px; margin-bottom: 20px; border: 6px solid #53667f; border-radius: 16px; background: linear-gradient(90deg, #60a5fa 50%, #f59e0b 0) 0 0 / 32px 32px; box-shadow: 0 12px 34px rgba(0,0,0,.25); transform: rotate(-3deg); }
.editor-status { min-height: 34px; display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 6px 13px; color: #536176; border-top: 1px solid #c9d3e0; background: #f7f9fc; font-size: .78rem; }

.inspector { overflow-y: auto; padding: 16px; scrollbar-color: #b7c4d6 transparent; }
.inspector section + section { margin-top: 20px; padding-top: 18px; border-top: 1px solid var(--line); }
.state-badge { padding: 4px 8px; border-radius: 999px; font-size: .72rem; font-weight: 800; }
.state-badge.clean { color: #536176; background: #e9eef5; }
.state-badge.dirty { color: var(--amber); background: var(--amber-soft); }
.state-badge.saved { color: var(--green); background: var(--green-soft); }
.tool-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.tool-button { min-height: 62px; display: flex; align-items: center; gap: 9px; padding: 9px; border: 1px solid var(--line); border-radius: 10px; color: var(--text); background: #f8fafc; cursor: pointer; text-align: left; }
.tool-button:hover { border-color: #a9b9cf; background: white; }
.tool-button.active { color: #1649a7; border-color: #74a0ed; background: #eaf2ff; box-shadow: inset 0 0 0 1px rgba(37,99,235,.1); }
.tool-button b { width: 25px; color: var(--blue); font-size: 1.25rem; text-align: center; }
.tool-button span { display: grid; gap: 2px; font-size: .86rem; font-weight: 750; }
.tool-button[data-tool="select"] { grid-column: 1 / -1; min-height: 50px; }
.region-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 8px; }
.region-actions .mini-button { min-height: 38px; padding: 6px 8px; font-size: .78rem; }
.region-actions kbd { display: block; margin-top: 2px; }
.region-hint { grid-column: 1 / -1; color: var(--muted); font-size: .7rem; line-height: 1.35; text-align: center; }
kbd { color: var(--muted); font-family: inherit; font-size: .68rem; font-weight: 650; }
.section-title { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 10px; }
.section-title span { color: var(--muted); font-size: .75rem; }

.palette-list { display: grid; grid-template-columns: 1fr 1fr; gap: 7px; max-height: 218px; overflow-y: auto; }
.palette-item { display: grid; grid-template-columns: 32px minmax(0, 1fr); align-items: center; gap: 7px; min-width: 0; padding: 5px; border: 2px solid transparent; border-radius: 9px; background: #f5f7fa; cursor: pointer; }
.palette-item.selected { border-color: var(--blue); background: #eaf2ff; }
.palette-item input { width: 32px; height: 32px; padding: 0; overflow: hidden; border: 0; border-radius: 6px; background: transparent; cursor: pointer; }
.palette-item span { min-width: 0; display: grid; color: var(--ink); font-size: .79rem; font-weight: 800; }
.palette-item small { overflow: hidden; color: var(--muted); font-size: .64rem; text-overflow: ellipsis; }

.balance-notice { margin-bottom: 9px; padding: 10px; border-radius: 9px; font-size: .81rem; font-weight: 750; line-height: 1.4; }
.balance-notice.neutral { color: #57657a; background: #edf1f6; }
.balance-notice.good { color: #06623f; background: var(--green-soft); }
.balance-notice.warning { color: var(--amber); background: var(--amber-soft); }
.balance-notice.danger { color: var(--red); background: var(--red-soft); }
.bus-source-summary { margin: -2px 2px 9px; color: var(--muted); font-size: .72rem; font-weight: 650; line-height: 1.45; }
.balance-table-wrap { max-height: 250px; overflow: auto; border: 1px solid var(--line); border-radius: 9px; }
.balance-table { width: 100%; border-collapse: collapse; font-size: .75rem; }
.balance-table th { position: sticky; top: 0; z-index: 1; padding: 7px 6px; color: #536176; background: #e8eef6; text-align: right; }
.balance-table th:first-child, .balance-table td:first-child { text-align: left; }
.balance-table td { padding: 7px 6px; border-top: 1px solid #e3e8ef; text-align: right; }
.balance-table tr.good { color: var(--green); background: #f3fcf7; }
.balance-table tr.short { color: var(--red); background: #fff5f4; }
.balance-table tr.excess { color: var(--amber); background: #fffaf0; }
.color-dot { display: inline-block; width: 11px; height: 11px; margin-right: 5px; border: 1px solid rgba(0,0,0,.16); border-radius: 3px; vertical-align: -1px; }

.toast-region { position: fixed; right: 18px; bottom: 18px; z-index: 50; display: grid; gap: 8px; pointer-events: none; }
.toast { max-width: min(420px, calc(100vw - 36px)); padding: 11px 14px; border: 1px solid #b8c8df; border-radius: 10px; color: var(--ink); background: white; box-shadow: 0 14px 34px rgba(20,35,58,.2); font-size: .86rem; font-weight: 700; animation: toast-in .2s ease both; }
.toast.success { border-color: #8bd5b1; background: #effbf5; }
.toast.warning { border-color: #e8c17c; background: #fff8e8; }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

@media (max-width: 1180px) {
  .topbar { grid-template-columns: auto 1fr; }
  .session-summary { grid-column: 1 / -1; grid-row: 2; display: flex; justify-content: space-between; }
  .workspace { grid-template-columns: 205px minmax(390px, 1fr) 292px; }
  .view-tools .mini-button:nth-child(-n+2) { display: none; }
}

@media (max-width: 860px) {
  body { overflow: auto; }
  .app-shell { height: auto; min-height: 100vh; }
  .topbar { position: sticky; top: 0; grid-template-columns: 1fr; gap: 10px; padding: 10px 12px; }
  .brand { display: none; }
  .top-actions { overflow-x: auto; padding-bottom: 2px; }
  .session-summary { grid-column: auto; grid-row: auto; }
  .workspace { display: flex; flex-direction: column; padding: 8px; }
  .levels-panel { min-height: 168px; max-height: 190px; }
  .levels-panel .panel-heading { padding-block: 9px; }
  .level-gallery { display: flex; overflow-x: auto; overflow-y: hidden; gap: 7px; }
  .gallery-empty { min-width: 100%; min-height: 110px; }
  .level-card { min-width: 126px; width: 126px; grid-template-columns: 1fr; justify-items: center; margin: 0; text-align: center; }
  .thumb-canvas { width: 74px; height: 74px; }
  .editor-panel { min-height: 66vh; }
  .editor-toolbar { align-items: flex-start; flex-wrap: wrap; }
  .canvas-viewport { min-height: 50vh; }
  .inspector { overflow: visible; }
  .inspector { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
  .inspector section + section { margin: 0; padding: 0; border: 0; }
  .balance-section { grid-column: 1 / -1; }
}

@media (max-width: 560px) {
  .button { min-height: 38px; padding-inline: 11px; font-size: .83rem; }
  .session-summary { align-items: flex-start; flex-direction: column; }
  .editor-toolbar { padding: 8px; }
  .view-tools { width: 100%; justify-content: center; }
  .level-nav { width: 100%; justify-content: center; }
  .toggle { display: none; }
  .background-control span { display: none; }
  .inspector { display: block; }
  .inspector section + section { margin-top: 18px; padding-top: 16px; border-top: 1px solid var(--line); }
  .palette-list { grid-template-columns: repeat(3, 1fr); }
  .palette-item { grid-template-columns: 1fr; justify-items: center; text-align: center; }
  .palette-item input { width: 42px; height: 36px; }
}

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