*,
*::before,
*::after {
  box-sizing: border-box;
}

:root {
  --bg: #ebf1f7;
  --surface: rgba(255, 255, 255, 0.86);
  --surface-strong: #ffffff;
  --edge: #d4deea;
  --edge-strong: #b9c7d8;
  --ink: #1e2a3a;
  --muted: #65758a;
  --accent: #0ea5b7;
  --accent-strong: #0b7f8d;
  --shadow: 0 10px 30px rgba(15, 23, 42, 0.12);
  --glass: blur(10px);
}

html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background:
    radial-gradient(circle at 0% 0%, #def5ff 0%, transparent 35%),
    radial-gradient(circle at 100% 100%, #e1f8ef 0%, transparent 40%), var(--bg);
  color: var(--ink);
  font-family: "Manrope", "Segoe UI", sans-serif;
}

#app {
  width: 100%;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#toolbar {
  height: 52px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 6px 10px;
  border-bottom: 1px solid var(--edge);
  background: linear-gradient(160deg, #ffffff 0%, #f7fbff 100%);
  box-shadow: 0 6px 22px rgba(13, 26, 48, 0.08);
  z-index: 15;
}

.toolbar-left {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.toolbar-brand {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 4px 10px;
  border-radius: 999px;
  background: linear-gradient(140deg, #0f2538, #182f42);
  color: #effaff;
  border: 1px solid #0f273d;
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.02em;
}

.toolbar-brand i {
  color: #86f2ff;
  font-size: 12px;
}

.brand-text {
  display: inline-flex;
  align-items: baseline;
  gap: 6px;
}

.brand-text span {
  opacity: 0.75;
  font-size: 9px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.toolbar-actions {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  overflow-x: auto;
}

#toolbar #toggle-toolbar-btn {
  display: none;
  flex-shrink: 0;
}

#toggle-toolbar-btn.active {
  border-color: #8da2b8;
  background: #dde6f1;
  color: #2e435a;
}

#app[data-topbar-collapsed="true"] .toolbar-actions {
  display: none;
}

@media (min-width: 901px) {
  #app[data-topbar-collapsed="true"] .toolbar-actions {
    display: inline-flex;
  }
}

#toolbar button,
#upload-sticker-label {
  appearance: none;
  border: 1px solid var(--edge-strong);
  background: var(--surface-strong);
  color: #334255;
  border-radius: 999px;
  min-height: 32px;
  padding: 5px 10px;
  font-size: 11px;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  transition:
    transform 0.14s ease,
    border-color 0.18s ease,
    background 0.18s ease;
}

#toolbar button:hover,
#upload-sticker-label:hover {
  transform: translateY(-1px);
  border-color: #88abc8;
  background: #f1f8ff;
}

#sticker-upload {
  display: none;
}

.icon {
  width: 12px;
  min-width: 12px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  line-height: 1;
}

#grid-wrapper {
  position: relative;
  flex: 1;
  overflow: auto;
  display: block;
  background:
    radial-gradient(circle at 10% 15%, #ffffff 0%, transparent 35%),
    radial-gradient(circle at 90% 85%, #deecfb 0%, transparent 38%), #eaf1f8;
}

#grid {
  display: block;
  margin: 0;
  image-rendering: pixelated;
  touch-action: none;
  user-select: none;
  background: #f0f4fa;
  box-shadow: inset 0 0 0 1px #d7e1ed;
}

#grid.view-pan {
  cursor: grab;
}

#grid.view-pan.panning {
  cursor: grabbing;
}

#left-dock,
#right-dock {
  position: fixed;
  top: calc(var(--toolbar-offset, 0px) + 14px);
  z-index: 20;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}

#left-dock {
  left: 14px;
  width: 232px;
}

#right-dock {
  right: 14px;
  width: 188px;
  bottom: 14px;
  max-height: calc(100dvh - var(--toolbar-offset, 0px) - 28px);
  overflow: hidden;
}

.dock-panel {
  pointer-events: auto;
  border: 1px solid var(--edge);
  border-radius: 14px;
  background: var(--surface);
  backdrop-filter: var(--glass);
  box-shadow: var(--shadow);
  padding: 8px;
}

[data-tool-card] {
  display: none;
}

#right-dock[data-tool-card] {
  display: none;
}

#app[data-active-tool="draw"] [data-tool-card~="draw"],
#app[data-active-tool="fill"] [data-tool-card~="fill"],
#app[data-active-tool="shape"] [data-tool-card~="shape"],
#app[data-active-tool="sticker"] [data-tool-card~="sticker"],
#app[data-active-tool="text"] [data-tool-card~="text"] {
  display: block;
}

#app[data-active-tool="sticker"] #right-dock[data-tool-card~="sticker"] {
  display: flex;
}

#app[data-ui-hidden="true"] #left-dock,
#app[data-ui-hidden="true"] #right-dock {
  display: none !important;
}

.panel-title {
  margin: 0 0 6px;
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.toolbar-group {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 6px;
  border: 1px solid var(--edge);
  border-radius: 10px;
  background: #f9fbff;
}

.toolbar-label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-width: 52px;
  font-size: 9px;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

#tool-group {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 4px;
  padding: 4px;
  border: 1px solid var(--edge);
  border-radius: 11px;
  background: #f6faff;
}

#tool-group .tool-btn {
  border: 1px solid #c7d5e6;
  border-radius: 9px;
  background: #ffffff;
  color: #324258;
  min-height: 34px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0;
  padding: 0;
}

#tool-group .tool-btn .btn-text {
  display: none;
}

#tool-group .tool-btn.active {
  border-color: #087483;
  background: linear-gradient(145deg, var(--accent), var(--accent-strong));
  color: #ffffff;
}

#capture-sticker-btn.active {
  border-color: #087483;
  background: linear-gradient(145deg, var(--accent), var(--accent-strong));
  color: #ffffff;
}

#toolbar #pan-view-btn.active,
#toolbar #pan-view-btn.active:hover,
#toolbar #pan-view-btn.active:active,
#toolbar #pan-view-btn.active:focus-visible {
  border-color: #087483;
  background: linear-gradient(145deg, var(--accent), var(--accent-strong));
  color: #123342;
}

#toggle-ui-btn.active {
  border-color: #8da2b8;
  background: #dde6f1;
  color: #2e435a;
}

#color-picker {
  width: 24px;
  height: 24px;
  padding: 1px;
  border: 1px solid var(--edge-strong);
  border-radius: 7px;
  cursor: pointer;
  background: none;
}

#brush-size,
#text-size {
  width: 86px;
  height: 6px;
  appearance: none;
  border-radius: 999px;
  outline: none;
  cursor: pointer;
}

#brush-size {
  background: linear-gradient(90deg, #7ddfe8, #0ea5b7);
}

#text-size {
  background: linear-gradient(90deg, #91d4ff, #0f8de5);
}

#brush-size::-webkit-slider-thumb,
#text-size::-webkit-slider-thumb {
  appearance: none;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 1px solid #0f5660;
  background: #0f6874;
}

#brush-size::-moz-range-thumb,
#text-size::-moz-range-thumb {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 1px solid #0f5660;
  background: #0f6874;
}

#brush-size-label,
#text-size-label {
  min-width: 18px;
  text-align: center;
  font-size: 10px;
  font-weight: 800;
  color: var(--muted);
}

#shape-type,
#shape-mode {
  height: 26px;
  border: 1px solid var(--edge-strong);
  border-radius: 8px;
  background: #fff;
  color: #334459;
  font-size: 11px;
  font-weight: 700;
  font-family: Arial, Helvetica, sans-serif;
  padding: 0 6px;
}

.shape-group {
  display: grid;
  grid-template-columns: auto 1fr 1fr;
  width: 100%;
}

.sticker-actions {
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex-shrink: 0;
}

.sticker-actions .action-btn,
.sticker-actions #upload-sticker-label {
  width: 100%;
  justify-content: center;
  border-radius: 9px;
  border: 1px solid var(--edge-strong);
  background: #ffffff;
  color: #334459;
  min-height: 32px;
}

#sticker-tray {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px;
  flex: 1;
  min-height: 0;
  max-height: none;
  overflow: auto;
}

.sticker-item {
  width: 100%;
  height: 54px;
  border: 1px solid #c6d4e3;
  border-radius: 10px;
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 3px;
  cursor: pointer;
}

.sticker-item.active {
  border-color: #087483;
  box-shadow: inset 0 0 0 1px #087483;
}

.sticker-thumb {
  width: 100%;
  height: 100%;
  object-fit: contain;
  image-rendering: pixelated;
}

#grid.tool-fill {
  cursor: cell;
}

#grid.tool-shape {
  cursor: crosshair;
}

#grid.tool-text {
  cursor: text;
}

@media (max-width: 1180px) {
  #toolbar {
    height: auto;
    flex-wrap: wrap;
    justify-content: center;
  }

  .toolbar-actions {
    order: 2;
    width: 100%;
    justify-content: center;
  }

  #left-dock,
  #right-dock {
    width: 170px;
  }

  #left-dock {
    left: 10px;
  }

  #right-dock {
    right: 10px;
  }

  .toolbar-label {
    min-width: 46px;
  }

  .shape-group {
    grid-template-columns: 1fr;
    gap: 5px;
  }
}

@media (max-width: 900px) {
  #toolbar {
    height: auto;
    padding: 8px;
    gap: 8px;
    flex-direction: column;
    align-items: stretch;
  }

  .toolbar-left {
    width: 100%;
    justify-content: space-between;
    order: 1;
  }

  .toolbar-brand {
    order: 1;
    display: inline-flex;
    width: auto;
    justify-content: flex-start;
  }

  #toolbar #toggle-toolbar-btn {
    order: 2;
    display: inline-flex;
  }

  .toolbar-actions {
    order: 2;
    width: 100%;
    justify-content: flex-start;
    flex-wrap: wrap;
    gap: 6px;
  }

  #toolbar button,
  #upload-sticker-label {
    min-height: 34px;
    padding: 6px 10px;
    font-size: 12px;
  }

  #toolbar .toolbar-actions button:hover,
  #toolbar .toolbar-actions button:active,
  #toolbar .toolbar-actions button:focus-visible {
    transform: none;
  }

  #left-dock {
    position: fixed;
    top: calc(var(--toolbar-offset, 0px) + 8px);
    left: 8px;
    right: 8px;
    width: auto;
    flex-direction: column;
    gap: 8px;
    overflow: visible;
    padding: 0;
    z-index: 24;
  }

  #right-dock {
    position: fixed;
    left: 8px;
    right: 8px;
    bottom: 8px;
    top: auto;
    width: auto;
    max-height: 42%;
    flex-direction: column;
    gap: 8px;
    overflow: hidden;
    padding: 0;
    z-index: 24;
  }

  #grid-wrapper {
    display: block;
    padding: 0;
    overflow: auto;
  }

  .dock-panel {
    padding: 8px;
    border-radius: 12px;
    min-width: 0;
    width: 100%;
  }

  #grid {
    max-width: none;
    max-height: none;
  }

  .tool-panel {
    min-width: 0;
  }

  .panel-title {
    display: block;
    margin-bottom: 6px;
  }

  #sticker-tray {
    display: flex;
    flex-wrap: nowrap;
    align-items: stretch;
    gap: 6px;
    flex: 1;
    min-height: 0;
    max-height: none;
    overflow-x: auto;
    overflow-y: hidden;
    min-width: 0;
    width: 100%;
  }

  .sticker-item {
    flex: 0 0 56px;
    width: 56px;
    min-width: 56px;
    height: 54px;
  }
}

@media (max-width: 640px) {
  #toolbar {
    padding: 6px;
  }

  .toolbar-actions {
    gap: 5px;
  }

  #grid-wrapper {
    padding: 0;
  }

  #left-dock,
  #right-dock {
    gap: 6px;
  }

  .dock-panel {
    padding: 7px;
  }

  .toolbar-label {
    min-width: 46px;
    font-size: 9px;
  }

  #tool-group .btn-text {
    display: none;
  }

  #tool-group .tool-btn {
    min-width: 36px;
  }

  #shape-type,
  #shape-mode {
    font-size: 10px;
  }
}
