:root {
  --bg: #fff7e6;
  --panel: #ffffff;
  --ink: #263238;
  --muted: #607d8b;
  --border: #e7dfd2;
  --primary: #ff8a3d;
  --primary-dark: #e96f22;
  --danger: #d84343;
  --shadow: 0 14px 40px rgba(71, 47, 20, 0.12);
  --radius: 20px;
}

* {
  box-sizing: border-box;
}

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

body {
  font-family: Inter, ui-rounded, "Segoe UI", system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
  background: var(--bg);
  color: var(--ink);
  overscroll-behavior: none;
}

button,
input {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-header {
  min-height: 84px;
  padding: 14px clamp(14px, 3vw, 34px);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  background: rgba(255, 255, 255, 0.9);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 30;
  backdrop-filter: blur(14px);
}

.app-header h1 {
  margin: 0;
  font-size: clamp(1.4rem, 2.5vw, 2rem);
}

.app-header p {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 0.92rem;
}

.header-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.button {
  border: 0;
  border-radius: 14px;
  padding: 11px 17px;
  font-weight: 800;
  min-height: 44px;
}

.button.primary {
  background: var(--primary);
  color: white;
}

.button.primary:hover {
  background: var(--primary-dark);
}

.button.secondary {
  background: #f4efe8;
  color: var(--ink);
}

.app-shell {
  display: grid;
  grid-template-columns: minmax(250px, 310px) 1fr;
  min-height: calc(100vh - 84px);
}

.toolbar {
  padding: 18px;
  background: var(--panel);
  border-right: 1px solid var(--border);
  overflow-y: auto;
}

.tool-section + .tool-section {
  margin-top: 22px;
}

.tool-section h2 {
  margin: 0 0 10px;
  font-size: 0.86rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}

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

.tool-button,
.small-action,
.size-button {
  border: 2px solid transparent;
  background: #f7f5f1;
  color: var(--ink);
  border-radius: 15px;
  min-height: 48px;
  font-weight: 750;
}

.tool-button {
  padding: 10px 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
}

.tool-icon {
  font-size: 1.15rem;
}

.tool-button.selected,
.size-button.selected {
  border-color: var(--primary);
  background: #fff1e7;
}

.color-palette {
  display: grid;
  grid-template-columns: repeat(6, minmax(32px, 1fr));
  gap: 9px;
}

.color-button {
  width: 100%;
  aspect-ratio: 1;
  border-radius: 50%;
  border: 3px solid white;
  box-shadow: 0 0 0 1px #d6cec3;
}

.color-button.selected {
  box-shadow: 0 0 0 3px var(--primary);
  transform: scale(1.05);
}

.custom-color {
  margin-top: 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-weight: 700;
  color: var(--muted);
}

.custom-color input {
  width: 58px;
  height: 38px;
  border: 0;
  background: none;
}

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

.size-button {
  display: grid;
  place-items: center;
  padding: 8px;
}

.size-button span {
  display: block;
  border-radius: 50%;
  background: var(--ink);
}

.action-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.small-action {
  padding: 9px 8px;
  display: grid;
  place-items: center;
  text-align: center;
}

.small-action:hover {
  background: #efebe5;
}

.small-action.danger {
  color: var(--danger);
}

.upload-action input {
  display: none;
}

.canvas-panel {
  min-width: 0;
  padding: clamp(12px, 2vw, 24px);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.canvas-info {
  min-height: 44px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 0 4px;
  font-weight: 700;
  color: var(--muted);
}

.opacity-control {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.86rem;
}

.canvas-stage {
  position: relative;
  width: 100%;
  flex: 1;
  min-height: 500px;
  background: white;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  touch-action: none;
}

.canvas-stage canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

#drawingCanvas {
  cursor: crosshair;
  touch-action: none;
}

.dialog {
  width: min(980px, calc(100vw - 28px));
  max-height: min(84vh, 840px);
  border: 0;
  border-radius: 24px;
  padding: 0;
  box-shadow: 0 28px 90px rgba(0, 0, 0, 0.28);
}

.dialog::backdrop {
  background: rgba(20, 25, 30, 0.52);
  backdrop-filter: blur(3px);
}

.dialog-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 22px;
  border-bottom: 1px solid var(--border);
}

.dialog-header h2,
.dialog-header p {
  margin: 0;
}

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

.icon-button {
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 50%;
  background: #f3eee7;
  font-size: 1.6rem;
}

.gallery-grid {
  padding: 20px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(210px, 1fr));
  gap: 16px;
  overflow-y: auto;
  max-height: calc(84vh - 90px);
}

.gallery-card {
  border: 1px solid var(--border);
  border-radius: 18px;
  overflow: hidden;
  background: white;
}

.gallery-card img {
  display: block;
  width: 100%;
  height: 150px;
  object-fit: contain;
  background: white;
}

.gallery-card-body {
  padding: 12px;
}

.gallery-card h3 {
  margin: 0 0 4px;
  font-size: 1rem;
}

.gallery-card time {
  color: var(--muted);
  font-size: 0.78rem;
}

.gallery-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 7px;
  margin-top: 10px;
}

.gallery-actions button {
  border: 0;
  border-radius: 10px;
  padding: 9px 7px;
  font-weight: 750;
}

.open-drawing {
  background: #fff1e7;
  color: #a8490d;
}

.delete-drawing {
  background: #fff0f0;
  color: var(--danger);
}

.empty-state {
  grid-column: 1 / -1;
  text-align: center;
  padding: 50px 20px;
  color: var(--muted);
}

.small-dialog {
  width: min(440px, calc(100vw - 28px));
  padding: 22px;
}

.small-dialog h2 {
  margin-top: 0;
}

.small-dialog label {
  display: grid;
  gap: 8px;
  font-weight: 750;
}

.small-dialog input {
  width: 100%;
  border: 1px solid #d7cec2;
  border-radius: 12px;
  padding: 12px 13px;
}

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

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translate(-50%, 20px);
  padding: 11px 16px;
  border-radius: 999px;
  background: #263238;
  color: white;
  font-weight: 750;
  opacity: 0;
  pointer-events: none;
  transition: 0.2s ease;
  z-index: 100;
}

.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

@media (max-width: 840px) {
  .app-header {
    align-items: flex-start;
  }

  .app-header p {
    display: none;
  }

  .app-shell {
    display: flex;
    flex-direction: column;
    min-height: calc(100vh - 75px);
  }

  .toolbar {
    order: 2;
    border-right: 0;
    border-top: 1px solid var(--border);
    display: grid;
    grid-auto-flow: column;
    grid-auto-columns: minmax(215px, 250px);
    gap: 18px;
    overflow-x: auto;
    overflow-y: hidden;
    padding: 13px;
    position: sticky;
    bottom: 0;
    z-index: 25;
    box-shadow: 0 -12px 32px rgba(50, 35, 20, 0.08);
  }

  .tool-section + .tool-section {
    margin-top: 0;
  }

  .canvas-panel {
    order: 1;
    padding: 10px;
    min-height: calc(100vh - 235px);
  }

  .canvas-stage {
    min-height: min(68vh, 720px);
  }

  .canvas-info {
    min-height: 38px;
    font-size: 0.84rem;
  }
}

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

  .app-header h1 {
    font-size: 1.2rem;
  }

  .header-actions {
    gap: 6px;
  }

  .button {
    min-height: 40px;
    padding: 9px 11px;
    border-radius: 11px;
    font-size: 0.86rem;
  }

  .canvas-panel {
    min-height: calc(100vh - 220px);
  }

  .canvas-stage {
    min-height: 60vh;
    border-radius: 15px;
  }

  .opacity-control {
    display: none;
  }

  .toolbar {
    grid-auto-columns: 210px;
  }
}
