:root {
  --ink: #17202a;
  --muted: #667085;
  --line: #d9e0e8;
  --bg: #eef3f7;
  --panel: #ffffff;
  --accent: #2563eb;
  --accent-soft: #eff6ff;
  --ok: #16845b;
  --warn: #b45309;
  --tool-cell: 18px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: Arial, "Malgun Gothic", sans-serif;
}

button {
  font: inherit;
}

button {
  border: 1px solid #b9c5d3;
  border-radius: 6px;
  padding: 6px 8px;
  background: #f8fafc;
  color: var(--ink);
  cursor: pointer;
  font-size: 13px;
  font-weight: 700;
}

button.primary,
#inferBtn {
  border-color: #1d4ed8;
  background: var(--accent);
  color: white;
}

button.active {
  border-color: #1d4ed8;
  background: var(--accent-soft);
  color: #1d4ed8;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

.app {
  max-width: none;
  margin: 0 auto;
  padding: 10px 264px 36px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 6px;
  margin-bottom: 4px;
}

h1,
h2,
h3,
p {
  margin: 0;
}

h1 {
  font-size: 20px;
  line-height: 1.18;
}

h2 {
  font-size: 16px;
  line-height: 1.15;
}

.topbar p,
#sketchHint,
#gridHint,
#twin1Hint,
#twin2Hint {
  color: var(--muted);
  margin-top: 2px;
  font-size: 12px;
  line-height: 1.25;
}

.topbar p {
  display: none;
}

.statusPill,
.panelStatus {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  border: 1px solid #bfdbfe;
  border-radius: 999px;
  padding: 0 5px;
  background: #eff6ff;
  color: #1d4ed8;
  font-size: 12px;
  font-weight: 800;
  white-space: nowrap;
}

.panelStatus {
  border-color: #d1d5db;
  background: #f8fafc;
  color: var(--muted);
}

.panelStatus.running {
  border-color: #fcd34d;
  background: #fffbeb;
  color: var(--warn);
}

.panelStatus.done {
  border-color: #86efac;
  background: #f0fdf4;
  color: var(--ok);
}

.workspace {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  grid-template-rows: auto auto;
  gap: 4px;
  align-items: stretch;
}

.panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  padding: 6px;
  min-width: 0;
  box-shadow: 0 1px 2px rgba(16, 24, 40, 0.04);
}

.primaryPanel {
  grid-column: 1;
  grid-row: 1 / span 2;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.twinGridPanel {
  grid-column: 2;
  grid-row: 1;
}

.twinFreePanel {
  grid-column: 3;
  grid-row: 1;
}

.sketchPanel,
.gridPanel {
  display: flex;
  flex-direction: column;
  height: 100%;
  background: linear-gradient(180deg, #ffffff 0%, #fbfcfd 100%);
}

.sketchPanel {
  grid-column: 2;
  grid-row: 2;
}

.gridPanel {
  grid-column: 3;
  grid-row: 2;
}

.sketchPanel canvas,
.gridPanel canvas,
.twinPanel canvas {
  flex: 0 0 auto;
  align-self: center;
  max-width: none;
  border-color: #cbd5e1;
  box-shadow: inset 0 0 0 1px #f8fafc, 0 8px 20px rgba(15, 23, 42, 0.05);
}

.twinPanel canvas,
.gridPanel canvas {
  width: 100%;
}

.sketchPanel .actions,
.gridPanel .actions {
  margin-top: 8px;
}

.sketchPanel #sketchHint,
.gridPanel #gridHint {
  margin-top: 4px;
  padding-top: 0;
}

.sketchPanel .panelHead,
.gridPanel .panelHead {
  padding-bottom: 5px;
  border-bottom: 1px solid #eef2f6;
}

.panelHead {
  display: flex;
  justify-content: space-between;
  gap: 6px;
  align-items: center;
  margin-bottom: 5px;
}

.headActions {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  justify-content: flex-end;
}

.sketchPanel .headActions,
.gridPanel .headActions {
  flex-wrap: nowrap;
}

.toolLabel {
  display: grid;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.toolRail {
  display: grid;
  grid-template-columns: max-content max-content;
  grid-template-areas:
    "shapeLabel colorLabel"
    "shapes colors";
  gap: 4px 5px;
  align-content: start;
  justify-items: center;
}

.toolRail .toolLabel:nth-of-type(1) {
  grid-area: shapeLabel;
}

.toolRail .toolLabel:nth-of-type(2) {
  grid-area: colorLabel;
}

.shapeButtons {
  grid-area: shapes;
  display: grid;
  grid-template-columns: var(--tool-cell);
  gap: 4px;
  justify-content: center;
  padding: 4px;
  border: 1px solid #eef2f6;
  border-radius: 8px;
  background: #f8fafc;
}

.shapeButton {
  width: var(--tool-cell);
  height: var(--tool-cell);
  padding: 0;
  display: grid;
  place-items: center;
  border-radius: 8px;
  font-size: 13px;
  line-height: 1;
}

.palette {
  grid-area: colors;
  display: grid;
  grid-template-columns: var(--tool-cell);
  gap: 4px;
  justify-content: center;
  padding: 4px;
  border: 1px solid #eef2f6;
  border-radius: 8px;
  background: #f8fafc;
}

.swatch {
  width: var(--tool-cell);
  height: var(--tool-cell);
  border: 2px solid #fff;
  border-radius: 50%;
  box-shadow: 0 0 0 1px #aeb8c4;
}

.swatch.active {
  box-shadow: 0 0 0 3px var(--accent);
}

.swatch:disabled {
  cursor: not-allowed;
  opacity: 0.28;
  filter: grayscale(1);
}

canvas {
  display: block;
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.sketchStage {
  display: grid;
  grid-template-columns: max-content minmax(0, 1fr);
  gap: 5px;
  justify-content: center;
  align-items: start;
}

.sketchStage .toolLabel {
  margin-bottom: 0;
}

#sketchCanvas {
  width: 100%;
  aspect-ratio: 1;
}

#gridCanvas {
  aspect-ratio: 1;
}

#twinGridCanvas,
#twinFreeCanvas {
  aspect-ratio: 1;
  cursor: grab;
  touch-action: none;
  user-select: none;
}

#twinGridCanvas:active,
#twinFreeCanvas:active {
  cursor: grabbing;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 6px 0 0;
}

.primaryPanel .actions {
  justify-content: flex-end;
}

.previewWrap {
  width: 100%;
  aspect-ratio: 1;
  min-height: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfcfd;
  display: grid;
  place-items: center;
  margin: 5px 0;
}

.previewWrap img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  aspect-ratio: 1;
  image-rendering: pixelated;
  background: #fff;
}

.previewWrap img:not([src]),
.previewWrap img[src=""] {
  visibility: hidden;
}

.resultBlock {
  margin-top: 5px;
}

.resultGrid {
  display: grid;
  gap: 5px;
}

.outputPanel .resultGrid {
  flex: 0 1 auto;
  grid-template-rows: none;
  min-height: 0;
}

.outputPanel .resultBlock {
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.resultBlock h3 {
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 4px;
}

pre {
  min-height: 34px;
  max-height: 54px;
  margin: 0;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #f8fafc;
  padding: 6px;
  font-size: 12px;
  line-height: 1.25;
}

.outputPanel pre {
  flex: 0 1 auto;
  max-height: none;
  min-height: 34px;
  height: auto;
}

@media (max-width: 780px) {
  :root {
    --tool-cell: 24px;
  }

  .app {
    max-width: none;
    width: 100%;
    padding: 10px 12px 28px;
  }

  .topbar {
    align-items: stretch;
    flex-direction: column;
    gap: 6px;
    margin-bottom: 8px;
  }

  h1 {
    font-size: 18px;
    line-height: 1.2;
  }

  h2 {
    font-size: 15px;
  }

  .statusPill {
    align-self: flex-start;
  }

  .workspace {
    grid-template-columns: 1fr;
    grid-template-rows: none;
    gap: 8px;
    align-items: start;
  }

  .primaryPanel,
  .twinGridPanel,
  .twinFreePanel,
  .sketchPanel,
  .gridPanel {
    grid-column: 1;
    grid-row: auto;
    height: auto;
  }

  .sketchPanel {
    order: 1;
  }

  .gridPanel {
    order: 2;
  }

  .primaryPanel {
    order: 3;
  }

  .twinGridPanel {
    order: 4;
  }

  .twinFreePanel {
    order: 5;
  }

  .panel {
    padding: 8px;
  }

  .panelHead,
  .sketchPanel .panelHead,
  .gridPanel .panelHead,
  .outputPanel .panelHead {
    align-items: center;
    flex-direction: row;
    gap: 8px;
    margin-bottom: 6px;
  }

  .headActions {
    gap: 6px;
  }

  button {
    min-height: 32px;
    padding: 6px 9px;
    font-size: 13px;
  }

  .previewWrap {
    width: min(100%, 260px);
    margin: 6px auto;
  }

  .twinPanel canvas,
  .gridPanel canvas {
    width: min(100%, 260px);
  }

  #sketchCanvas {
    width: min(100%, 240px);
  }

  .sketchStage {
    grid-template-columns: max-content minmax(0, 1fr);
    gap: 8px;
    justify-content: center;
  }

  .toolRail {
    gap: 4px 6px;
  }

  .shapeButtons,
  .palette {
    padding: 5px;
  }

  .resultGrid {
    gap: 6px;
  }

  pre {
    max-height: 96px;
  }

  #sketchHint,
  #gridHint,
  #twin1Hint,
  #twin2Hint {
    font-size: 12px;
    line-height: 1.3;
  }
}

@media (max-width: 520px) {
  :root {
    --tool-cell: 22px;
  }

  .app {
    padding: 8px 8px 24px;
  }

  h1 {
    font-size: 17px;
  }

  .panelHead,
  .sketchPanel .panelHead,
  .gridPanel .panelHead,
  .outputPanel .panelHead {
    flex-wrap: wrap;
  }

  .previewWrap,
  .twinPanel canvas,
  .gridPanel canvas {
    width: min(100%, 230px);
  }

  #sketchCanvas {
    width: min(100%, 210px);
  }

  .sketchStage {
    gap: 6px;
  }

  .headActions {
    justify-content: flex-start;
  }
}
