:root {
  --bg: #eef0ed;
  --surface: #fbfaf6;
  --surface-strong: #ffffff;
  --surface-muted: #f1eee7;
  --line: #d4d0c7;
  --line-strong: #b8b2a7;
  --text: #26231f;
  --text-soft: #69635a;
  --accent: #477f76;
  --accent-soft: #dbece7;
  --accent-strong: #2f655d;
  --save: #e2ead6;
  --shadow: rgba(45, 41, 35, 0.12);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
}

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

body {
  overflow: hidden;
  color: var(--text);
  background: var(--bg);
  font-family: "Apple SD Gothic Neo", "Malgun Gothic", system-ui, sans-serif;
  touch-action: none;
}

button,
select,
input {
  font: inherit;
}

button,
select {
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-strong);
}

button {
  cursor: pointer;
  box-shadow: 0 1px 2px var(--shadow);
}

button:disabled {
  opacity: 0.45;
}

button:active:not(:disabled) {
  transform: translateY(1px);
  box-shadow: none;
}

.app-shell {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(320px, 42vw);
  gap: 16px;
  width: 100vw;
  height: 100dvh;
  padding: max(14px, env(safe-area-inset-top)) max(14px, env(safe-area-inset-right)) max(14px, env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left));
}

.workspace,
.palette-box,
.tool-area,
.history-box,
.layers,
.tool-rail {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(251, 250, 246, 0.94);
  box-shadow: 0 14px 34px var(--shadow);
}

.workspace {
  display: grid;
  place-items: center;
  min-width: 0;
  min-height: 0;
  padding: 18px;
  overflow: hidden;
}

#drawCanvas {
  display: block;
  width: min(100%, 86dvh);
  height: auto;
  max-height: 100%;
  aspect-ratio: 1;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  background: #fff;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.72), 0 10px 28px rgba(47, 43, 37, 0.14);
  image-rendering: pixelated;
  touch-action: none;
}

.control-panel {
  display: grid;
  grid-template-rows: 270px minmax(0, 1fr);
  gap: 16px;
  min-width: 0;
  min-height: 0;
}

.palette-box {
  display: grid;
  grid-template-columns: 92px minmax(0, 1fr) 136px 116px;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 10px 14px;
  padding: 14px;
}

.panel-title {
  grid-column: 1 / -1;
  color: var(--text-soft);
  font-size: 15px;
  font-weight: 700;
  letter-spacing: 0;
  line-height: 1;
}

.palette {
  grid-column: 2 / -1;
  grid-row: 2;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-auto-rows: 44px;
  gap: 8px;
}

.swatch {
  width: 100%;
  height: 100%;
  min-width: 0;
  padding: 0;
  border-color: rgba(38, 35, 31, 0.2);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.45);
}

.swatch.is-active {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

.custom-color {
  grid-column: 1;
  grid-row: 2;
  display: grid;
  gap: 8px;
  justify-items: center;
  align-content: center;
  color: var(--text-soft);
  font-size: 14px;
  font-weight: 700;
}

.custom-color input {
  width: 68px;
  height: 68px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-strong);
  box-shadow: 0 1px 2px var(--shadow);
}

.palette-actions {
  grid-column: 4;
  grid-row: 3;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.palette-actions button {
  min-height: 36px;
  min-width: 0;
  color: var(--accent-strong);
  font-size: 18px;
  font-weight: 700;
  background: var(--accent-soft);
}

.palette-actions button.is-confirmed {
  color: #236347;
  background: #cfe9d9;
}

#menuToggleButton {
  display: none;
}

.tool-area {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 12px;
  min-height: 0;
  padding: 14px;
}

.top-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 12px;
  align-items: center;
}

.top-row strong {
  color: var(--text);
  font-size: 22px;
  letter-spacing: 0;
}

.close-menu-button {
  display: none;
  width: 42px;
  min-height: 38px;
  font-size: 24px;
}

.history-box {
  grid-column: 3;
  grid-row: 3;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  padding: 6px;
  background: var(--surface-muted);
  box-shadow: none;
}

.history-box button {
  min-height: 38px;
  color: var(--text-soft);
  font-size: 15px;
  font-weight: 800;
}

.brush-control {
  grid-column: 1 / 3;
  grid-row: 3;
  display: grid;
  grid-template-columns: 72px minmax(0, 1fr);
  gap: 10px;
  align-items: center;
  color: var(--text-soft);
  font-size: 14px;
  font-weight: 800;
}

.brush-control input[type="range"] {
  width: 100%;
  min-height: 34px;
  accent-color: var(--accent);
}

.lower-row {
  display: grid;
  grid-template-columns: 132px minmax(0, 1fr);
  gap: 14px;
  min-height: 0;
}

.layers {
  display: grid;
  grid-template-rows: auto repeat(4, 1fr);
  gap: 10px;
  min-height: 0;
  padding: 12px;
  color: var(--text-soft);
  text-align: center;
  font-size: 15px;
  font-weight: 800;
  background: var(--surface-muted);
  box-shadow: none;
}

.layer-button,
.tool-button {
  min-height: 48px;
  padding: 5px 8px;
  font-size: 17px;
  font-weight: 700;
  line-height: 1.08;
  word-break: keep-all;
}

.icon-button {
  display: inline-grid;
  place-items: center;
  padding: 0;
}

.utility-icon-button {
  display: inline-grid;
  place-items: center;
  padding: 0;
}

.tool-icon {
  width: 30px;
  height: 30px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.9;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.art-icon {
  width: 40px;
  height: 40px;
  stroke-width: 3;
}

.art-icon .tile {
  fill: #eef3f0;
  stroke: #cfd9d4;
}

.art-icon .shadow-shape {
  fill: rgba(38, 35, 31, 0.16);
  stroke: none;
}

.brush-icon .metal,
.picker-icon .glass,
.picker-icon .dropper {
  fill: #edf0ef;
  stroke: #59635e;
}

.brush-icon .paint {
  fill: #477f76;
  stroke: #2f655d;
}

.brush-icon .shine {
  stroke: #ffffff;
}

.eraser-icon .rubber {
  fill: #f3b5bd;
  stroke: #8b5960;
}

.eraser-icon .band,
.eraser-icon .crumb {
  stroke: #ffffff;
}

.picker-icon .liquid,
.picker-icon .drop {
  fill: #3aa7ff;
  stroke: #2d6e9e;
}

.fill-icon .bucket {
  fill: #f1d28d;
  stroke: #745e2c;
}

.fill-icon .paint-wave,
.fill-icon .drop {
  fill: #64c68e;
  stroke: #2f7a51;
}

.fill-icon .handle {
  stroke: #745e2c;
}

.move-icon .arrow {
  stroke: #5c6f7b;
}

.move-icon .head,
.move-icon .center-dot {
  fill: #8ea4ad;
  stroke: #5c6f7b;
}

.flip-icon .mirror,
.flip-icon .dash {
  stroke: #5d6275;
}

.flip-icon .left-shape {
  fill: #88b7d5;
  stroke: #4e7089;
}

.flip-icon .right-shape {
  fill: #c7d8e5;
  stroke: #718999;
}

.layer-button {
  touch-action: none;
}

.tool-rail {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  grid-template-rows: repeat(4, minmax(42px, 1fr));
  gap: 10px;
  min-height: 0;
  padding: 12px;
  box-shadow: none;
}

.tool-button.is-active,
.layer-button.is-active {
  color: var(--accent-strong);
  border-color: #a8c9c1;
  background: var(--accent-soft);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.74), 0 1px 2px var(--shadow);
}

.tool-button.save {
  background: var(--save);
}

.settings-row {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
}

.settings-row label {
  display: grid;
  gap: 6px;
  color: var(--text-soft);
  font-size: 14px;
  font-weight: 700;
}

.settings-row select {
  width: 100%;
  min-height: 38px;
}

@media (max-width: 900px) {
  body {
    overflow: hidden;
  }

  .app-shell {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(0, 1fr) auto;
    gap: 8px;
    padding: max(8px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
  }

  .workspace {
    padding: 8px;
  }

  #drawCanvas {
    width: min(100%, calc(100dvh - 184px));
    max-height: 100%;
  }

  .control-panel {
    display: block;
    min-height: 0;
  }

  .palette-box {
    grid-template-columns: 64px minmax(0, 1fr) 116px;
    grid-template-rows: auto auto auto;
    gap: 7px;
    padding: 8px;
    box-shadow: 0 8px 22px var(--shadow);
  }

  .panel-title {
    display: none;
  }

  .custom-color {
    grid-column: 1;
    grid-row: 1;
    gap: 5px;
    font-size: 12px;
  }

  .palette {
    grid-column: 2 / -1;
    grid-row: 1;
    grid-template-columns: repeat(8, minmax(20px, 1fr));
    grid-auto-rows: 28px;
    gap: 5px;
  }

  .custom-color input {
    width: 46px;
    height: 46px;
  }

  .brush-control {
    grid-column: 1 / 3;
    grid-row: 2;
    grid-template-columns: 58px minmax(0, 1fr);
    min-width: 0;
    font-size: 12px;
  }

  .history-box {
    grid-column: 3;
    grid-row: 2;
    padding: 0;
    gap: 5px;
    background: transparent;
    border: 0;
  }

  .history-box button {
    min-height: 34px;
    padding: 0;
    font-size: 12px;
  }

  .palette-actions {
    grid-column: 1 / -1;
    grid-row: 3;
    grid-template-columns: repeat(3, 44px);
    justify-content: end;
    gap: 7px;
  }

  .palette-actions button {
    min-height: 34px;
    padding: 0;
    font-size: 17px;
  }

  #menuToggleButton {
    display: block;
  }

  .tool-area {
    position: fixed;
    inset: max(10px, env(safe-area-inset-top)) max(10px, env(safe-area-inset-right)) max(10px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
    z-index: 10;
    display: none;
    grid-template-rows: auto minmax(0, 1fr) auto;
    padding: 12px;
    background: rgba(251, 250, 246, 0.98);
    box-shadow: 0 22px 60px rgba(45, 41, 35, 0.28);
  }

  .control-panel.menu-open .tool-area {
    display: grid;
  }

  .close-menu-button {
    display: block;
  }

  .lower-row {
    grid-template-columns: 1fr;
  }

  .layers {
    grid-template-columns: auto repeat(4, 1fr);
    grid-template-rows: 44px;
    align-items: center;
  }

  .tool-rail {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    grid-template-rows: repeat(2, 44px);
  }

  .tool-button,
  .layer-button {
    min-height: 42px;
    font-size: 15px;
  }

  .tool-icon {
    width: 28px;
    height: 28px;
  }

  .art-icon {
    width: 34px;
    height: 34px;
  }
}

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

  .workspace {
    padding: 6px;
  }

  .palette {
    grid-template-columns: repeat(8, minmax(16px, 1fr));
    grid-auto-rows: 24px;
  }

  .top-row,
  .settings-row {
    grid-template-columns: 1fr;
  }

  .brush-control {
    grid-template-columns: 54px minmax(0, 1fr);
  }

  .tool-rail {
    grid-template-columns: repeat(2, 1fr);
    grid-template-rows: repeat(4, 42px);
  }

  .tool-icon {
    width: 26px;
    height: 26px;
  }

  .art-icon {
    width: 32px;
    height: 32px;
  }
}
