:root {
  color-scheme: light;
  --bg: #fff7ed;
  --panel: #ffffff;
  --ink: #243042;
  --muted: #677383;
  --line: #e6dccb;
  --accent: #18a999;
  --accent-strong: #108a7d;
  --warn: #d94841;
  --shadow: 0 18px 48px rgba(70, 48, 24, 0.14);
  --radius: 8px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  font-family: Inter, ui-rounded, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  background:
    linear-gradient(135deg, rgba(24, 169, 153, 0.15), transparent 30%),
    linear-gradient(315deg, rgba(255, 196, 87, 0.16), transparent 34%),
    var(--bg);
  color: var(--ink);
  overscroll-behavior: none;
  touch-action: pan-x pan-y;
  -webkit-font-smoothing: antialiased;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

body {
  position: fixed;
  inset: 0;
}

button,
input {
  font: inherit;
}

button {
  -webkit-tap-highlight-color: transparent;
}

.app-shell {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  width: 100vw;
  height: 100dvh;
  padding: calc(14px + var(--safe-top)) calc(14px + var(--safe-right)) calc(14px + var(--safe-bottom)) calc(14px + var(--safe-left));
  gap: 12px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  min-height: 64px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.brand-mark {
  width: 44px;
  height: 44px;
  flex: 0 0 auto;
  border-radius: 50%;
  background:
    radial-gradient(circle at 32% 30%, #fff 0 8%, transparent 9%),
    conic-gradient(from 20deg, #ff6b6b, #ffd166, #06d6a0, #4cc9f0, #b388ff, #ff6b6b);
  box-shadow: inset 0 0 0 4px rgba(255, 255, 255, 0.58), 0 10px 24px rgba(72, 48, 27, 0.15);
}

h1,
h2,
p {
  margin: 0;
}

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

.brand p {
  margin-top: 5px;
  color: var(--muted);
  font-size: 0.94rem;
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.tool-row {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
}

.icon-button,
.tool-button {
  display: inline-grid;
  place-items: center;
  width: 48px;
  height: 48px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(255, 255, 255, 0.88);
  color: var(--ink);
  box-shadow: 0 6px 16px rgba(73, 48, 25, 0.08);
  cursor: pointer;
  touch-action: manipulation;
}

.tool-button {
  width: 100%;
  height: 76px;
  grid-template-rows: 38px auto;
  gap: 4px;
  align-content: center;
  padding: 8px 6px;
}

.icon-button:active,
.tool-button:active,
.text-button:active {
  transform: translateY(1px);
}

.icon-button:disabled {
  opacity: 0.42;
  cursor: default;
}

.icon-button svg,
.tool-button svg {
  width: 24px;
  height: 24px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.tool-button svg {
  width: 34px;
  height: 34px;
}

.tool-button span {
  display: block;
  max-width: 100%;
  overflow: hidden;
  font-size: 0.82rem;
  font-weight: 850;
  line-height: 1.05;
  text-align: center;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.icon-button.danger {
  color: var(--warn);
}

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 244px;
  grid-template-areas: "canvas tools";
  gap: 12px;
  min-height: 0;
}

.menu-button {
  flex: 0 0 auto;
}

.page-drawer {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  z-index: 30;
  display: flex;
  flex-direction: column;
  width: min(320px, 86vw);
  padding: calc(14px + var(--safe-top)) 14px calc(14px + var(--safe-bottom)) calc(14px + var(--safe-left));
  border-right: 1px solid rgba(230, 220, 203, 0.88);
  background: rgba(255, 251, 244, 0.98);
  box-shadow: 22px 0 60px rgba(53, 36, 16, 0.22);
  backdrop-filter: blur(20px);
  transform: translateX(-105%);
  transition: transform 0.28s ease;
  overflow: hidden;
}

.page-drawer.open {
  transform: translateX(0);
}

.drawer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding-bottom: 12px;
  border-bottom: 1px solid rgba(230, 220, 203, 0.6);
}

.page-drawer .page-grid {
  flex: 1;
  margin-top: 12px;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding-right: 4px;
}

.drawer-backdrop {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: rgba(36, 24, 12, 0.32);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.24s ease;
}

.drawer-backdrop.open {
  opacity: 1;
  pointer-events: auto;
}

.panel {
  min-height: 0;
  border: 1px solid rgba(230, 220, 203, 0.88);
  border-radius: var(--radius);
  background: rgba(255, 255, 255, 0.76);
  box-shadow: var(--shadow);
  backdrop-filter: blur(18px);
}

.tools-panel {
  grid-area: tools;
  display: flex;
  flex-direction: column;
  justify-content: start;
  height: 100%;
  padding: 16px;
  gap: 18px;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: none;
}

.tools-panel::-webkit-scrollbar {
  display: none;
}

.panel-title {
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 750;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.page-grid {
  display: grid;
  gap: 10px;
  margin-top: 10px;
}

.page-card {
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr);
  align-items: center;
  gap: 10px;
  width: 100%;
  min-height: 60px;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fffdf8;
  color: var(--ink);
  text-align: left;
  cursor: pointer;
}

.page-card.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(24, 169, 153, 0.2);
}

.page-thumb {
  width: 44px;
  height: 44px;
  border: 1px solid #d8cfbf;
  border-radius: 6px;
  background: #fff;
}

.page-name {
  min-width: 0;
  overflow: hidden;
  font-size: 0.94rem;
  font-weight: 750;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.canvas-wrap {
  grid-area: canvas;
  display: grid;
  place-items: center;
  min-width: 0;
  min-height: 0;
}

.paper {
  position: relative;
  width: min(100%, calc((100dvh - 118px - var(--safe-top) - var(--safe-bottom)) * 1.294));
  max-width: 100%;
  aspect-ratio: 11 / 8.5;
  overflow: hidden;
  border: 1px solid #dbd1c2;
  border-radius: var(--radius);
  background: #fff;
  box-shadow: 0 22px 70px rgba(61, 43, 25, 0.22);
  touch-action: none;
}

.paper > canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
}

#lineCanvas {
  pointer-events: none;
}

.tool-button.active {
  border-color: var(--accent);
  background: #e8fbf8;
  color: var(--accent-strong);
}

.range-field {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  color: var(--muted);
  font-size: 0.92rem;
  font-weight: 750;
}

output {
  min-width: 28px;
  color: var(--ink);
  text-align: right;
}

.slider {
  width: 100%;
  accent-color: var(--accent);
}

.colors-title {
  margin-top: 2px;
}

.swatches {
  display: grid;
  grid-template-columns: repeat(3, 52px);
  justify-content: center;
  gap: 13px;
  padding: 6px;
}

.swatch {
  position: relative;
  width: 52px;
  aspect-ratio: 1;
  border: 2px solid rgba(36, 48, 66, 0.12);
  border-radius: 999px;
  background: var(--swatch);
  cursor: pointer;
  box-shadow: inset 0 0 0 3px rgba(255, 255, 255, 0.5);
}

.swatch.active::after {
  position: absolute;
  inset: -6px;
  border: 4px solid var(--ink);
  border-radius: inherit;
  content: "";
}

.text-button {
  min-height: 46px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fffdf8;
  color: var(--ink);
  font-weight: 800;
  cursor: pointer;
  touch-action: manipulation;
}

.text-button.quiet {
  color: var(--muted);
}

.text-button.danger-fill {
  border-color: var(--warn);
  background: var(--warn);
  color: #fff;
}

.confirm-dialog {
  width: min(92vw, 360px);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 0;
  box-shadow: 0 24px 80px rgba(34, 27, 19, 0.25);
}

.confirm-dialog::backdrop {
  background: rgba(36, 48, 66, 0.28);
  backdrop-filter: blur(3px);
}

.confirm-dialog form {
  padding: 20px;
}

.confirm-dialog h2 {
  font-size: 1.3rem;
  letter-spacing: 0;
}

.confirm-dialog p {
  margin-top: 8px;
  color: var(--muted);
}

.dialog-actions {
  display: flex;
  justify-content: end;
  gap: 10px;
  margin-top: 22px;
}

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

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

  .topbar {
    min-height: 56px;
  }

  .brand p {
    display: none;
  }

  .workspace {
    grid-template-columns: 1fr;
    grid-template-rows: auto minmax(0, 1fr);
    grid-template-areas:
      "tools"
      "canvas";
    overflow: hidden;
  }

  .tools-panel {
    display: grid;
    grid-template-columns: auto minmax(110px, 1fr) minmax(150px, 1.4fr) auto;
    align-items: center;
    gap: 10px;
    padding: 10px;
  }

  .tool-row {
    grid-template-columns: repeat(3, 48px);
    gap: 8px;
  }

  .tool-button {
    width: 48px;
    height: 48px;
    grid-template-rows: 1fr;
    padding: 0;
  }

  .tool-button svg {
    width: 25px;
    height: 25px;
  }

  .tool-button span {
    display: none;
  }

  .range-field,
  .colors-title {
    display: none;
  }

  .swatches {
    grid-template-columns: repeat(auto-fit, minmax(34px, 36px));
    overflow: visible;
    padding: 5px;
  }

  .swatch {
    width: 36px;
  }

  .slider {
    min-width: 0;
  }

  .paper {
    width: min(100%, calc((100dvh - 158px - var(--safe-top) - var(--safe-bottom)) * 1.294));
  }
}

@media (max-width: 640px) {
  .app-shell {
    padding: calc(10px + var(--safe-top)) calc(10px + var(--safe-right)) calc(10px + var(--safe-bottom)) calc(10px + var(--safe-left));
    gap: 8px;
  }

  .brand-mark {
    width: 38px;
    height: 38px;
  }

  h1 {
    font-size: 1.28rem;
  }

  .top-actions {
    gap: 6px;
  }

  .icon-button,
  .tool-button {
    width: 42px;
    height: 42px;
  }

  .tool-row {
    grid-template-columns: repeat(2, 42px);
  }

  .tools-panel {
    grid-template-columns: auto minmax(88px, 1fr) minmax(104px, 1.3fr);
  }

  .text-button {
    display: none;
  }

  .swatches {
    grid-template-columns: repeat(auto-fit, minmax(30px, 32px));
  }

  .swatch {
    width: 32px;
  }

  .paper {
    width: min(100%, calc((100dvh - 218px - var(--safe-top) - var(--safe-bottom)) * 1.294));
  }
}
