:root {
  --bg-app: #232322;
  --bg-panel: #2b2b2a;
  --bg-elevated: #343432;
  --bg-input: #1d1d1c;
  --border: #3f3f3d;
  --border-focus: #00aeef;
  --text-main: #f3f4f6;
  --text-muted: #a8a8a5;
  --accent: #00aeef;
  --accent-hover: #0095cf;
  --danger: #e05252;
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 14px;
  --font-ui: 'Noto Sans Armenian', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

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

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg-app);
  color: var(--text-main);
  font-family: var(--font-ui);
  font-size: 14px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

[hidden] {
  display: none !important;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
}

/* --- AUTH VIEW --- */
.auth-view {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px;
  background: var(--bg-app);
}

.auth-card {
  width: 100%;
  max-width: 400px;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 32px;
}

.auth-brand {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 12px;
  margin-bottom: 24px;
}

.auth-logo-img {
  height: 52px;
  width: auto;
  object-fit: contain;
  display: block;
}

.auth-brand p {
  margin: 0;
  font-size: 13px;
  color: var(--text-muted);
}

.auth-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* --- FORMS & INPUTS --- */
.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

label {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
}

input[type="text"],
input[type="password"],
input[type="email"],
select,
textarea {
  width: 100%;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-main);
  font-family: inherit;
  font-size: 13.5px;
  padding: 9px 11px;
  outline: none;
  transition: border-color 0.15s;
}

input:focus, select:focus, textarea:focus {
  border-color: var(--border-focus);
}

textarea {
  resize: vertical;
  min-height: 60px;
}

.password-wrap {
  position: relative;
  display: flex;
  align-items: center;
}

.password-wrap input {
  padding-right: 40px;
}

.password-wrap .icon-btn {
  position: absolute;
  right: 6px;
}

.form-error {
  margin: 0;
  padding: 9px 12px;
  border-radius: var(--radius-sm);
  background: rgba(224, 82, 82, 0.14);
  border: 1px solid rgba(224, 82, 82, 0.4);
  color: #fca5a5;
  font-size: 13px;
}

/* --- BUTTONS --- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  padding: 9px 14px;
  font-family: inherit;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
  white-space: nowrap;
}

.btn-primary {
  background: var(--accent);
  color: #111111;
}
.btn-primary:hover {
  background: var(--accent-hover);
}

.btn-secondary {
  background: var(--bg-elevated);
  border-color: var(--border);
  color: var(--text-main);
}
.btn-secondary:hover {
  border-color: #555552;
  background: #3c3c3a;
}
.btn-secondary.active {
  border-color: var(--accent);
  background: var(--bg-input);
  color: var(--accent);
}

.btn-ghost {
  background: transparent;
  color: var(--text-muted);
}
.btn-ghost:hover {
  color: var(--text-main);
  background: var(--bg-elevated);
}

.btn-danger {
  background: rgba(224, 82, 82, 0.16);
  border-color: rgba(224, 82, 82, 0.35);
  color: #fca5a5;
}
.btn-danger:hover {
  background: rgba(224, 82, 82, 0.28);
}

.btn-sm {
  padding: 6px 10px;
  font-size: 12px;
}

.btn-full {
  width: 100%;
}

.icon-btn {
  background: transparent;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 4px 7px;
  border-radius: 6px;
  font-size: 15px;
  line-height: 1;
}
.icon-btn:hover {
  color: var(--text-main);
  background: var(--bg-elevated);
}

/* --- STUDIO LAYOUT --- */
.studio-view {
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg-app);
}

.topbar {
  height: 58px;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  gap: 12px;
  flex-shrink: 0;
}

.topbar-left, .topbar-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-pill {
  display: flex;
  align-items: center;
  padding-right: 12px;
  border-right: 1px solid var(--border);
}

.topbar-logo-img {
  height: 28px;
  width: auto;
  object-fit: contain;
  display: block;
}

.client-bar {
  display: flex;
  align-items: center;
  gap: 6px;
}

.client-select {
  width: auto;
  min-width: 180px;
  max-width: 240px;
  padding: 6px 10px;
  font-weight: 600;
}

.format-switcher {
  display: inline-flex;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 3px;
  gap: 3px;
}

.format-btn {
  border: none;
  background: transparent;
  color: var(--text-muted);
  padding: 5px 12px;
  border-radius: 6px;
  font-family: inherit;
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.format-btn.active {
  background: var(--accent);
  color: #111111;
}

.format-tag {
  font-size: 11px;
  opacity: 0.85;
}

.workspace {
  flex: 1;
  display: grid;
  grid-template-columns: 375px 1fr;
  min-height: 0;
}

/* --- SIDEBAR --- */
.sidebar {
  background: var(--bg-panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.sidebar-tabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  border-bottom: 1px solid var(--border);
  background: var(--bg-input);
  flex-shrink: 0;
}

.tab-btn {
  border: none;
  background: transparent;
  color: var(--text-muted);
  padding: 11px 6px;
  font-family: inherit;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  border-bottom: 2px solid transparent;
}

.tab-btn.active {
  color: var(--text-main);
  border-bottom-color: var(--accent);
  background: var(--bg-panel);
}

.tab-panel {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.panel-group {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--border);
}

.panel-group:last-child {
  border-bottom: none;
}

.panel-group h3 {
  margin: 0;
  font-size: 12.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-main);
}

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

.inline-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.flex-1 {
  flex: 1;
}

.control-row {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.slider-control {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.val-badge {
  font-size: 11.5px;
  font-variant-numeric: tabular-nums;
  color: var(--text-main);
  background: var(--bg-elevated);
  padding: 2px 7px;
  border-radius: 5px;
}

input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
  cursor: pointer;
  margin: 0;
}

.switch {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  cursor: pointer;
  color: var(--text-main);
  font-size: 12.5px;
}

.switch input {
  width: 16px;
  height: 16px;
  accent-color: var(--accent);
  cursor: pointer;
}

.muted-note {
  margin: 0;
  font-size: 12px;
  color: var(--text-muted);
}

/* --- PRESET GRID --- */
.preset-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
}

.preset-btn {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: var(--radius-sm);
  padding: 8px 6px;
  font-family: inherit;
  font-size: 11.5px;
  font-weight: 600;
  cursor: pointer;
  text-align: center;
}

.preset-btn.active {
  border-color: var(--accent);
  background: var(--bg-input);
  color: var(--accent);
}

/* --- UPLOAD DROPZONE & ASSETS --- */
.upload-dropzone {
  border: 1.5px dashed var(--border);
  border-radius: var(--radius-md);
  padding: 16px 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 4px;
  cursor: pointer;
  background: var(--bg-input);
  transition: border-color 0.15s;
}

.upload-dropzone:hover,
.upload-dropzone.dragover {
  border-color: var(--accent);
}

.dropzone-icon {
  font-size: 22px;
}

.dropzone-title {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-main);
}

.dropzone-sub {
  font-size: 11px;
  color: var(--text-muted);
}

.assets-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.asset-card {
  position: relative;
  aspect-ratio: 1;
  border-radius: var(--radius-sm);
  border: 1.5px solid var(--border);
  overflow: hidden;
  cursor: pointer;
  background-color: #262625;
  background-image:
    linear-gradient(45deg, #30302e 25%, transparent 25%),
    linear-gradient(-45deg, #30302e 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #30302e 75%),
    linear-gradient(-45deg, transparent 75%, #30302e 75%);
  background-size: 14px 14px;
  background-position: 0 0, 0 7px, 7px -7px, -7px 0;
}

.asset-card.selected {
  border-color: var(--accent);
}

.asset-card img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  padding: 4px;
}

.asset-actions {
  position: absolute;
  inset: auto 4px 4px 4px;
  display: flex;
  justify-content: space-between;
  gap: 4px;
  opacity: 0;
  transition: opacity 0.15s;
}

.asset-card:hover .asset-actions {
  opacity: 1;
}

.asset-mini-btn {
  border: none;
  border-radius: 5px;
  background: rgba(25, 25, 24, 0.9);
  color: #fff;
  font-size: 11px;
  padding: 3px 6px;
  cursor: pointer;
}
.asset-mini-btn:hover {
  background: var(--accent);
  color: #111111;
}
.asset-mini-btn.del:hover {
  background: var(--danger);
  color: #fff;
}

/* --- HSL SLIDER COLOR PICKER --- */
.color-pickers-stack {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.hsl-picker {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 9px;
}

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

.hsl-title-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
}

.hsl-swatch {
  width: 24px;
  height: 24px;
  border-radius: 6px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  position: relative;
  overflow: hidden;
  flex-shrink: 0;
}

.hsl-swatch input[type="color"] {
  position: absolute;
  inset: -6px;
  width: 36px;
  height: 36px;
  opacity: 0;
  cursor: pointer;
}

.hsl-hex-input {
  width: 92px !important;
  padding: 5px 8px !important;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace !important;
  font-size: 12px !important;
  text-transform: uppercase;
}

.hsl-slider-row {
  display: grid;
  grid-template-columns: 18px 1fr 38px;
  align-items: center;
  gap: 8px;
}

.hsl-slider-row span {
  font-size: 11px;
  color: var(--text-muted);
  font-weight: 600;
}

.hsl-slider-row output {
  font-size: 11px;
  text-align: right;
  font-variant-numeric: tabular-nums;
  color: var(--text-muted);
}

.hsl-range {
  -webkit-appearance: none;
  appearance: none;
  height: 10px;
  border-radius: 999px;
  outline: none;
  border: 1px solid rgba(255, 255, 255, 0.14);
}

.hsl-range::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #fff;
  border: 2px solid #111;
  cursor: pointer;
}

.hsl-range.hue {
  background: linear-gradient(to right, #ff0000 0%, #ffff00 17%, #00ff00 33%, #00ffff 50%, #0000ff 67%, #ff00ff 83%, #ff0000 100%);
}

/* --- STAGE --- */
.stage {
  background: var(--bg-app);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 18px;
  gap: 12px;
  min-height: 0;
  overflow: hidden;
}

.stage-meta {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 12px;
  color: var(--text-muted);
}

.stage-client-pill {
  padding: 3px 10px;
  border-radius: 999px;
  font-weight: 600;
  background-color: var(--client-accent, #00aeef);
  color: #111111;
  color: color(from var(--client-accent, #00aeef) xyz-d65 calc(max(0, sign(0.17913 - y)) * 0.9505) max(0, sign(0.17913 - y)) calc(max(0, sign(0.17913 - y)) * 1.089));
  color: contrast-color(var(--client-accent, #00aeef));
}

.canvas-frame {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
}

#preview-canvas {
  max-height: 100%;
  max-width: 100%;
  width: auto;
  height: auto;
  border-radius: 8px;
  border: 1px solid var(--border);
  cursor: grab;
  touch-action: none;
}

#preview-canvas:active {
  cursor: grabbing;
}

/* --- DIALOGS (native <dialog> best practices) --- */
dialog {
  background: var(--bg-panel);
  color: var(--text-main);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 0;
  width: min(480px, calc(100vw - 32px));
  max-height: calc(100vh - 32px);
}

dialog:has(.dialog-wide) {
  width: min(860px, calc(100vw - 32px));
}

dialog:modal {
  overscroll-behavior: contain;
}

dialog::backdrop {
  background: rgba(18, 18, 17, 0.78);
  overflow: hidden;
  overscroll-behavior: contain;
}

.dialog-body {
  padding: 22px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.dialog-header h2 {
  margin: 0;
  font-size: 17px;
  font-weight: 700;
}

.dialog-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding-top: 8px;
}

.logo-upload-row {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo-preview-box {
  width: 56px;
  height: 56px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg-input);
  display: grid;
  place-items: center;
  font-size: 10px;
  color: var(--text-muted);
  overflow: hidden;
}

.logo-preview-box img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}

/* --- BG REMOVER --- */
.bg-remove-layout {
  display: grid;
  grid-template-columns: 1fr 270px;
  gap: 18px;
  min-height: 380px;
}

.bg-remove-canvas-wrap {
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  display: grid;
  place-items: center;
  overflow: hidden;
  max-height: 460px;
}

.checkerboard {
  background-color: #262625;
  background-image:
    linear-gradient(45deg, #30302e 25%, transparent 25%),
    linear-gradient(-45deg, #30302e 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #30302e 75%),
    linear-gradient(-45deg, transparent 75%, #30302e 75%);
  background-size: 20px 20px;
  background-position: 0 0, 0 10px, 10px -10px, -10px 0;
}

#bg-remove-canvas {
  max-width: 100%;
  max-height: 450px;
  object-fit: contain;
  cursor: crosshair;
}

.bg-remove-controls {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* Desktop hides mobile-only chrome */
.mobile-sheet-header,
.mobile-bottom-nav,
#tab-more {
  display: none;
}

.mobile-actions-grid {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

/* --- TOUCH & MOBILE ADAPTATION --- */
@media (pointer: coarse) {
  :root {
    --target-size: 44px;
  }

  :where(button, select, input:not([type="checkbox"], [type="radio"], [type="range"]), .switch) {
    min-block-size: var(--target-size);
  }

  /* Prevent iOS Safari auto-zoom on input focus */
  input[type="text"],
  input[type="password"],
  input[type="email"],
  select,
  textarea {
    font-size: 16px;
  }

  input[type="range"] {
    height: 30px;
  }

  .hsl-range {
    height: 18px !important;
  }

  .hsl-range::-webkit-slider-thumb {
    width: 24px;
    height: 24px;
  }
}

@media (max-width: 900px) {
  /* 1. Single compact top bar */
  .topbar {
    height: auto;
    min-height: 52px;
    padding: max(6px, env(safe-area-inset-top)) 10px 6px;
    gap: 8px;
    flex-wrap: nowrap;
  }

  .brand-pill,
  #edit-client-btn,
  #new-client-btn,
  #export-jpg-btn,
  #change-password-btn,
  #logout-btn {
    display: none;
  }

  .topbar-left {
    flex: 1;
    min-width: 0;
    gap: 0;
  }

  .client-bar {
    width: 100%;
    min-width: 0;
  }

  .client-select {
    width: 100%;
    min-width: 0;
    max-width: none;
    padding: 6px 8px;
    font-size: 13px;
  }

  .topbar-center {
    flex-shrink: 0;
  }

  .format-btn {
    padding: 6px 8px;
    font-size: 11px;
  }

  .format-tag {
    display: none;
  }

  .topbar-right {
    flex-shrink: 0;
    gap: 0;
  }

  #export-png-btn {
    padding: 7px 10px;
    font-size: 12px;
  }

  /* 2. Full-height canvas stage in the center */
  .workspace {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
    padding-bottom: calc(60px + env(safe-area-inset-bottom, 0px));
  }

  .stage {
    flex: 1;
    min-height: 0;
    padding: 10px 12px;
    gap: 6px;
  }

  .stage-meta {
    font-size: 11px;
    gap: 8px;
  }

  .stage-client-pill {
    display: none;
  }

  /* 3. Bottom sheet editor drawer (slides up when a tab is tapped) */
  .sidebar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: calc(60px + env(safe-area-inset-bottom, 0px));
    max-height: 62dvh;
    z-index: 40;
    border-right: none;
    border-top: 1px solid var(--border);
    border-radius: 16px 16px 0 0;
    transform: translateY(110%);
    transition: transform 0.22s ease;
    pointer-events: none;
  }

  .studio-view.mobile-sheet-open .sidebar {
    transform: translateY(0);
    pointer-events: auto;
  }

  .sidebar-tabs {
    display: none;
  }

  .mobile-sheet-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 14px;
    border-bottom: 1px solid var(--border);
    background: var(--bg-input);
    border-radius: 16px 16px 0 0;
    flex-shrink: 0;
  }

  #mobile-sheet-title {
    font-size: 13.5px;
    font-weight: 700;
    color: var(--text-main);
  }

  .tab-panel {
    padding: 14px 14px 20px;
    max-height: calc(62dvh - 54px);
    overflow-y: auto;
  }

  #tab-more:not([hidden]) {
    display: flex;
  }

  .preset-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  /* 4. Fixed mobile bottom navigation bar */
  .mobile-bottom-nav {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    height: calc(60px + env(safe-area-inset-bottom, 0px));
    padding-bottom: env(safe-area-inset-bottom, 0px);
    background: var(--bg-panel);
    border-top: 1px solid var(--border);
    z-index: 50;
  }

  .mobile-nav-btn {
    border: none;
    background: transparent;
    color: var(--text-muted);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    font-family: inherit;
    font-size: 11px;
    font-weight: 600;
    cursor: pointer;
    padding: 4px 2px;
  }

  .mobile-nav-btn .m-nav-icon {
    font-size: 17px;
    line-height: 1.1;
  }

  .mobile-nav-btn.active {
    color: var(--accent);
  }

  /* 5. Dialogs on small screens */
  dialog {
    width: calc(100vw - 20px);
    max-height: calc(100dvh - 20px);
  }

  dialog:has(.dialog-wide) {
    width: calc(100vw - 20px);
  }

  .dialog-body {
    padding: 16px;
    overflow-y: auto;
    max-height: calc(100dvh - 24px);
  }

  .bg-remove-layout {
    grid-template-columns: 1fr;
    min-height: 0;
  }

  .bg-remove-canvas-wrap {
    max-height: 260px;
  }

  #bg-remove-canvas {
    max-height: 250px;
  }
}
