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

:root {
  --sidebar-width: 220px;
  --bg-app: #1a1a1a;
  --bg-sidebar: #1c1c1e;
  --bg-canvas-area: #242424;
  --bg-input: #2c2c2e;
  --text-primary: #f2f2f7;
  --text-secondary: #8e8e93;
  --border: #38383a;
  --accent: #0a84ff;
  --btn-bg: #3a3a3c;
  --btn-hover: #48484a;
  --radius: 6px;
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif;
}

html, body {
  height: 100%;
  overflow: hidden;
}

body {
  font-family: var(--font);
  background: var(--bg-app);
  color: var(--text-primary);
  -webkit-font-smoothing: antialiased;
}

/* ── Layout ─────────────────────────────────────────────────────────────────── */

#app {
  display: flex;
  height: 100vh;
}

/* ── Sidebar ─────────────────────────────────────────────────────────────────── */

#sidebar {
  width: var(--sidebar-width);
  min-width: var(--sidebar-width);
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  overflow-x: hidden;
}

.sidebar-header {
  padding: 18px 16px 15px;
  border-bottom: 1px solid var(--border);
}

.sidebar-header h1 {
  font-size: 15px;
  font-weight: 600;
  letter-spacing: -0.2px;
}

.sidebar-footer {
  margin-top: auto;
  padding: 12px 16px;
}

.sidebar-footer p {
  font-size: 11px;
  color: var(--text-secondary);
}

.sidebar-section {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.section-label {
  display: block;
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.7px;
  color: var(--text-secondary);
  margin-bottom: 2px;
}

.subsection-label {
  display: block;
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.7px;
  color: var(--text-secondary);
  margin-top: 8px;
}

/* ── Inputs ──────────────────────────────────────────────────────────────────── */

input[type="text"],
input[type="number"] {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 13px;
  font-family: var(--font);
  padding: 7px 10px;
  outline: none;
  width: 100%;
  transition: border-color 0.15s;
}

input[type="text"]:focus,
input[type="number"]:focus {
  border-color: var(--accent);
}

input[type="text"]::placeholder {
  color: var(--text-secondary);
}

.input-row {
  display: flex;
  gap: 6px;
}

.input-row input {
  flex: 1;
  min-width: 0;
}

/* ── Buttons ─────────────────────────────────────────────────────────────────── */

button {
  background: var(--btn-bg);
  border: none;
  border-radius: var(--radius);
  color: var(--text-primary);
  cursor: pointer;
  font-size: 13px;
  font-family: var(--font);
  padding: 7px 12px;
  transition: background 0.15s;
  white-space: nowrap;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
}

button svg {
  width: 13px;
  height: 13px;
  flex-shrink: 0;
}

button:hover:not(:disabled) {
  background: var(--btn-hover);
}

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

button.accent {
  background: var(--accent);
}

button.accent:hover:not(:disabled) {
  background: #0071e3;
}

.full-btn {
  width: 100%;
  text-align: center;
}

.btn-row {
  display: flex;
  gap: 6px;
}

.half-btn {
  flex: 1;
  text-align: center;
}

/* ── Background toggle ───────────────────────────────────────────────────────── */

.bg-toggle {
  display: flex;
  gap: 6px;
}

.bg-btn {
  flex: 1;
}

.bg-btn.active {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

#bg-transparent {
  background-image:
    linear-gradient(45deg, #555 25%, transparent 25%),
    linear-gradient(-45deg, #555 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #555 75%),
    linear-gradient(-45deg, transparent 75%, #555 75%);
  background-size: 8px 8px;
  background-position: 0 0, 0 4px, 4px -4px, -4px 0;
  background-color: #333;
}

#bg-transparent:hover:not(:disabled) {
  background-color: #3a3a3a;
}

/* ── Margin row ──────────────────────────────────────────────────────────────── */

.margin-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.margin-row label {
  font-size: 13px;
  color: var(--text-secondary);
  white-space: nowrap;
}

.margin-row input {
  width: 68px;
  text-align: right;
}

.canvas-size-grid {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

/* ── Hint ────────────────────────────────────────────────────────────────────── */

.url-error {
  font-size: 11px;
  color: #ff453a;
  line-height: 1.4;
}

.hint {
  font-size: 11px;
  color: var(--text-secondary);
  line-height: 1.5;
}

kbd {
  background: #3a3a3c;
  border-radius: 3px;
  font-family: var(--font);
  font-size: 10px;
  line-height: 1;
  padding: 1px 4px;
}

/* ── Toast ───────────────────────────────────────────────────────────────────── */

#toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(6px);
  background: #3a3a3c;
  color: var(--text-primary);
  font-size: 13px;
  padding: 8px 16px;
  border-radius: 20px;
  opacity: 0;
  transition: opacity 0.2s ease, transform 0.2s ease;
  pointer-events: none;
  white-space: nowrap;
  z-index: 100;
}

#toast.visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ── Canvas area ─────────────────────────────────────────────────────────────── */

#canvas-area {
  flex: 1;
  position: relative;
  background: var(--bg-canvas-area);
  overflow: hidden;
}

#canvas-viewport {
  position: absolute;
  inset: 0;
  overflow: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 32px 32px 56px;
}

#canvas-stage {
  width: fit-content;
  min-width: 100%;
  min-height: 100%;
  display: grid;
  place-items: center;
}

#canvas-shell {
  position: relative;
}

/* Fabric.js wraps #c in a .canvas-container div */
.canvas-container {
  box-shadow: 0 8px 40px rgba(0, 0, 0, 0.6);
}

/* ── Empty state ─────────────────────────────────────────────────────────────── */

#empty-state {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}

#empty-state-desktop,
#empty-state-mobile {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

#empty-state-mobile {
  display: none;
  pointer-events: auto;
}

#empty-state p {
  font-size: 14px;
  color: var(--text-secondary);
  text-align: center;
  line-height: 1.6;
}

.empty-state-or {
  letter-spacing: 0.2px;
  font-size: 11px;
}

/* ── Shortcut bar ────────────────────────────────────────────────────────────── */

#shortcut-bar {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 4px 12px;
  padding: 7px 16px;
  background: var(--bg-sidebar);
  border-top: 1px solid var(--border);
  pointer-events: none;
}

.shortcut {
  font-size: 11px;
  color: var(--text-secondary);
  white-space: nowrap;
}

.shortcut-sep {
  width: 1px;
  height: 10px;
  background: var(--border);
  flex-shrink: 0;
}

/* ── Zoom controls ───────────────────────────────────────────────────────────── */

#zoom-controls {
  position: absolute;
  bottom: 52px;
  right: 16px;
  display: flex;
  align-items: stretch;
  background: var(--bg-sidebar);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  z-index: 5;
  pointer-events: auto;
}

#zoom-controls button {
  background: transparent;
  border: none;
  border-radius: 0;
  padding: 4px 8px;
  font-size: 12px;
  min-width: 30px;
  height: 26px;
}

#zoom-controls button + button {
  border-left: 1px solid var(--border);
}

#zoom-controls button svg {
  width: 11px;
  height: 11px;
}

/* ── Mobile toolbar ──────────────────────────────────────────────────────────── */

#mobile-toolbar {
  display: none;
}

/* ── Mobile bottom sheets ────────────────────────────────────────────────────── */

.mob-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 20;
}

.mob-sheet {
  display: none;
  position: fixed;
  bottom: 56px;
  left: 0;
  right: 0;
  background: var(--bg-sidebar);
  border-top: 1px solid var(--border);
  border-radius: 16px 16px 0 0;
  padding: 12px 16px 24px;
  z-index: 30;
  flex-direction: column;
  gap: 10px;
  max-height: min(70vh, 420px);
  overflow-y: auto;
}

.mob-sheet-handle {
  width: 36px;
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  margin: 0 auto 8px;
  flex-shrink: 0;
}

.mob-sheet-title {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.7px;
  color: var(--text-secondary);
}

.mob-overlay.visible { display: block; }
.mob-sheet.visible   { display: flex; }

/* ── Mobile breakpoint ───────────────────────────────────────────────────────── */

@media (max-width: 640px) {
  #sidebar      { display: none; }
  #shortcut-bar { display: none; }

  #mobile-toolbar {
    display: flex;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 56px;
    background: var(--bg-sidebar);
    border-top: 1px solid var(--border);
    align-items: center;
    justify-content: space-around;
    padding: 0 8px;
    z-index: 10;
  }

  #mobile-toolbar button {
    flex: 1;
    height: 44px;
    background: transparent;
    font-size: 11px;
    flex-direction: column;
    gap: 2px;
  }

  #mobile-toolbar button svg {
    width: 20px;
    height: 20px;
  }

  #canvas-viewport {
    padding: 12px 12px 72px;
  }

  .canvas-container {
    touch-action: none;
  }

  #empty-state-desktop {
    display: none;
  }

  #empty-state-mobile {
    display: flex;
  }

  #toast {
    bottom: 72px;
  }

  #zoom-controls {
    bottom: 68px;
  }
}
