:root {
  --bg: #0f1117;
  --panel: #171a23;
  --panel-2: #1e2230;
  --border: #2a2f40;
  --text: #e8eaf2;
  --muted: #9aa1b5;
  --accent: #6c8cff;
  --accent-2: #8f6cff;
  --radius: 12px;
}

* { box-sizing: border-box; }

@font-face {
  font-family: "FA6S";
  src: url("../fonts/fa-solid-900.woff2") format("woff2");
  font-weight: 900;
  font-style: normal;
  font-display: block;
  unicode-range: U+f000-f8ff;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: "Vazirmatn", "Segoe UI", Tahoma, sans-serif;
  font-size: 14px;
}

body { overflow: hidden; }

.ic {
  font-family: "FA6S";
  font-weight: 900;
  font-style: normal;
  display: inline-block;
  min-width: 1.1em;
  text-align: center;
}

.ic-chevron::before { content: "\f078"; }
.ic-check::before { content: "\f00c"; }
.ic-font::before { content: "\f031"; }
.ic-fontsize::before { content: "\f034"; }
.ic-palette::before { content: "\f53f"; }
.ic-vector::before { content: "\f125"; }
.ic-download::before { content: "\f019"; }
.ic-upload::before { content: "\f093"; }
.ic-align-right::before { content: "\f038"; }
.ic-align-center::before { content: "\f037"; }
.ic-align-left::before { content: "\f036"; }
.ic-expand::before { content: "\f065"; }
.ic-image::before { content: "\f03e"; }
.ic-code::before { content: "\f121"; }
.ic-gradient::before { content: "\f1fc"; }
.ic-stroke::before { content: "\f5aa"; }
.ic-shadow::before { content: "\f24d"; }
.ic-hand::before { content: "\f256"; }
.ic-dir-toggle::before { content: "\f337"; }

.app {
  display: grid;
  grid-template-columns: 390px 1fr;
  height: calc(100vh - var(--nav-h, 0px));
  height: calc(100dvh - var(--nav-h, 0px));
  direction: rtl;
}

.sidebar {
  overflow-y: auto;
  border-left: 1px solid var(--border);
  background: var(--panel);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.app-header h1 {
  margin: 0 0 4px;
  font-size: 20px;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.app-header h1 .ic { -webkit-text-fill-color: var(--accent); font-size: 16px; }

.app-header p {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.7;
}

.tabs { display: none; }

.panel {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.panel h2 {
  margin: 0;
  font-size: 13px;
  color: var(--accent);
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 6px;
}

textarea {
  width: 100%;
  background: #12141c;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  font-family: inherit;
  font-size: 13.5px;
  outline: none;
  resize: vertical;
  line-height: 1.8;
  transition: border-color 0.15s;
}

textarea:focus { border-color: var(--accent); }

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

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

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

.row { display: flex; gap: 10px; }
.grow { flex: 1; min-width: 0; }

.field {
  display: flex;
  flex-direction: column;
  gap: 5px;
  flex: 1;
  min-width: 0;
}

.field > span {
  color: var(--muted);
  font-size: 11px;
}

.checkbox {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  color: var(--text);
  font-size: 12.5px;
  user-select: none;
}

.checkbox input {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
  pointer-events: none;
}

.checkbox input + .cbox {
  position: relative;
  width: 18px;
  height: 18px;
  min-width: 18px;
  border: 2px solid var(--border);
  border-radius: 5px;
  background: #12141c;
  transition: all 0.15s;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.checkbox input + .cbox::after {
  content: "";
  width: 5px;
  height: 9px;
  border: solid #fff;
  border-width: 0 2.5px 2.5px 0;
  transform: rotate(45deg) scale(0);
  transition: transform 0.12s ease;
  margin-top: -2px;
}

.checkbox input:checked + .cbox {
  background: var(--accent);
  border-color: var(--accent);
}

.checkbox input:checked + .cbox::after {
  transform: rotate(45deg) scale(1);
}

.checkbox input:focus-visible + .cbox {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.checkbox:hover input:not(:checked) + .cbox {
  border-color: var(--accent);
}

.align-group { display: flex; gap: 6px; }

.align-group button {
  flex: 1;
  background: #12141c;
  border: 1px solid var(--border);
  color: var(--muted);
  border-radius: 8px;
  padding: 9px 4px;
  cursor: pointer;
  font-family: inherit;
  font-size: 13px;
  transition: all 0.15s;
  display: flex;
  align-items: center;
  justify-content: center;
}

.align-group button.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.toggle-block {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.toggle-body {
  display: none;
  flex-direction: column;
  gap: 10px;
}

.toggle-block.enabled .toggle-body { display: flex; }

.preset-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.preset-chips:empty { display: none; }

.preset-chip {
  background: #12141c;
  border: 1px solid var(--border);
  color: var(--muted);
  border-radius: 20px;
  padding: 5px 12px;
  font-family: inherit;
  font-size: 11.5px;
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
}

.preset-chip:hover {
  border-color: var(--accent);
  color: var(--text);
}

.dir-btns {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
}

.dir-btns button {
  flex: 1;
  background: #12141c;
  border: 1px solid var(--border);
  color: var(--muted);
  border-radius: 8px;
  padding: 8px 4px;
  font-family: inherit;
  font-size: 12px;
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
}

.dir-btns button:hover {
  border-color: var(--accent);
  color: var(--text);
}

.dir-btns button.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.upload-box {
  border: 1px dashed var(--border);
  border-radius: 10px;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.upload-title {
  color: var(--muted);
  font-size: 12px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.fpick { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }

.fpick-btn {
  background: #12141c;
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 8px 12px;
  font-family: inherit;
  font-size: 12.5px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 6px;
  transition: border-color 0.15s;
}

.fpick-btn:hover { border-color: var(--accent); }

.fpick-name {
  color: var(--accent);
  font-size: 11.5px;
  direction: ltr;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 180px;
  white-space: nowrap;
}

.btn {
  border: 1px solid var(--border);
  background: #12141c;
  color: var(--text);
  border-radius: 10px;
  padding: 11px 12px;
  font-family: inherit;
  font-size: 13.5px;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.15s;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.btn:hover { border-color: var(--accent); }

.btn.ghost { width: 100%; font-weight: 500; font-size: 12.5px; padding: 9px; }

.btn.primary {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  border: none;
  color: #fff;
}

.btn.primary:hover { filter: brightness(1.12); }

.export-panel { position: sticky; bottom: 0; }
.export-buttons { display: flex; gap: 10px; }

.hint {
  margin: 0;
  color: var(--muted);
  font-size: 11px;
  line-height: 1.9;
  display: flex;
  gap: 6px;
  align-items: flex-start;
}

.hint .ic { margin-top: 3px; color: var(--accent); }

.cselect { position: relative; }

.cselect-btn {
  width: 100%;
  background: #12141c;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px 12px;
  font-family: inherit;
  font-size: 13px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  transition: border-color 0.15s;
}

.cselect-btn:hover { border-color: var(--accent); }
.cselect.open .cselect-btn { border-color: var(--accent); }
.cselect-btn .ic { color: var(--muted); font-size: 10px; transition: transform 0.15s; }
.cselect.open .cselect-btn .ic { transform: rotate(180deg); }

.cselect-label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.cselect-menu {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  left: 0;
  max-height: 280px;
  overflow-y: auto;
  background: #141722;
  border: 1px solid var(--accent);
  border-radius: 10px;
  z-index: 50;
  padding: 6px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.55);
}

.cselect-group {
  color: var(--accent);
  font-size: 10.5px;
  padding: 8px 8px 4px;
  font-weight: 700;
}

.cselect-item {
  width: 100%;
  background: transparent;
  border: none;
  color: var(--text);
  text-align: right;
  padding: 8px 10px;
  border-radius: 7px;
  font-family: inherit;
  font-size: 12.5px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.cselect-item:hover { background: #1e2436; }
.cselect-item.selected { background: rgba(108, 140, 255, 0.16); color: #fff; }
.cselect-item .ic { color: var(--accent); font-size: 11px; }

.cselect-menu[hidden], .cpick-pop[hidden] { display: none !important; }

.cpick { position: relative; display: block; }

.cpick-swatch {
  width: 100%;
  height: 36px;
  border-radius: 8px;
  border: 1px solid var(--border);
  cursor: pointer;
  padding: 0;
  background-image:
    linear-gradient(45deg, #3a3f52 25%, transparent 25%),
    linear-gradient(-45deg, #3a3f52 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #3a3f52 75%),
    linear-gradient(-45deg, transparent 75%, #3a3f52 75%);
  background-size: 12px 12px;
  background-position: 0 0, 0 6px, 6px -6px, -6px 0;
  position: relative;
  overflow: hidden;
}

.cpick-swatch::after {
  content: "";
  position: absolute;
  inset: 0;
  background: var(--swatch-color, #fff);
}

.cpick.open .cpick-swatch { border-color: var(--accent); }

.cpick-pop {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  width: 250px;
  background: #141722;
  border: 1px solid var(--accent);
  border-radius: 12px;
  z-index: 60;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.55);
}

.cpick-sv {
  position: relative;
  width: 100%;
  height: 140px;
  border-radius: 8px;
  cursor: crosshair;
  overflow: hidden;
}

.cpick-sv-white {
  position: absolute;
  inset: 0;
  background: linear-gradient(to right, #fff, transparent);
}

.cpick-sv-black {
  position: absolute;
  inset: 0;
  background: linear-gradient(to top, #000, transparent);
}

.cpick-dot {
  position: absolute;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 2px solid #fff;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.5), inset 0 0 0 1px rgba(0, 0, 0, 0.3);
  transform: translate(-50%, -50%);
  pointer-events: none;
}

.cpick-hue {
  position: relative;
  height: 14px;
  border-radius: 7px;
  cursor: pointer;
}

.cpick-hue-dot {
  position: absolute;
  top: 50%;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  border: 2px solid #fff;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.4);
  transform: translate(-50%, -50%);
  pointer-events: none;
}

.cpick-row { display: flex; gap: 8px; }

.cpick-hex {
  flex: 1;
  background: #12141c;
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  padding: 7px 10px;
  font-family: monospace;
  font-size: 13px;
  direction: ltr;
  text-align: left;
  outline: none;
}

.cpick-hex:focus { border-color: var(--accent); }

.cpick-presets {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  gap: 5px;
}

.cpick-preset {
  aspect-ratio: 1;
  border-radius: 5px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  cursor: pointer;
  padding: 0;
}

.cpick-preset:hover { transform: scale(1.15); }

.preview-area {
  display: flex;
  flex-direction: column;
  min-width: 0;
  background:
    radial-gradient(1200px 600px at 70% 0%, rgba(108, 140, 255, 0.08), transparent),
    var(--bg);
}

.preview-meta {
  padding: 10px 18px;
  color: var(--muted);
  font-size: 12px;
  border-bottom: 1px solid var(--border);
}

.preview-stage {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  overflow: auto;
  min-height: 0;
}

.checker {
  position: relative;
  background-image:
    linear-gradient(45deg, #2a2e3a 25%, transparent 25%),
    linear-gradient(-45deg, #2a2e3a 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #2a2e3a 75%),
    linear-gradient(-45deg, transparent 75%, #2a2e3a 75%);
  background-size: 20px 20px;
  background-position: 0 0, 0 10px, 10px -10px, -10px 0;
  background-color: #20232e;
  border-radius: 4px;
  box-shadow: 0 12px 48px rgba(0, 0, 0, 0.5);
  line-height: 0;
}

#preview-canvas {
  display: block;
  max-width: min(70vw, calc(100vh - var(--nav-h, 0px) - 180px));
  max-height: calc(100vh - var(--nav-h, 0px) - 160px);
  width: auto;
  height: auto;
}

.handles {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.chandle {
  position: absolute;
  pointer-events: auto;
  z-index: 5;
}

.chandle::after {
  content: "";
  position: absolute;
  background: #fff;
  border: 1px solid var(--accent);
  border-radius: 3px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.45);
}

.chandle[data-dir="nw"], .chandle[data-dir="ne"], .chandle[data-dir="se"], .chandle[data-dir="sw"] {
  width: 16px;
  height: 16px;
}

.chandle[data-dir="nw"] { top: -8px; left: -8px; cursor: nwse-resize; }
.chandle[data-dir="se"] { bottom: -8px; right: -8px; cursor: nwse-resize; }
.chandle[data-dir="ne"] { top: -8px; right: -8px; cursor: nesw-resize; }
.chandle[data-dir="sw"] { bottom: -8px; left: -8px; cursor: nesw-resize; }

.chandle[data-dir="nw"]::after,
.chandle[data-dir="ne"]::after,
.chandle[data-dir="se"]::after,
.chandle[data-dir="sw"]::after {
  inset: 1px;
}

.chandle[data-dir="n"], .chandle[data-dir="s"] {
  width: 40px;
  height: 14px;
  left: 50%;
  transform: translateX(-50%);
  cursor: ns-resize;
}

.chandle[data-dir="n"] { top: -7px; }
.chandle[data-dir="s"] { bottom: -7px; }

.chandle[data-dir="n"]::after,
.chandle[data-dir="s"]::after {
  left: 8px;
  right: 8px;
  top: 4px;
  bottom: 4px;
}

.chandle[data-dir="e"], .chandle[data-dir="w"] {
  width: 14px;
  height: 40px;
  top: 50%;
  transform: translateY(-50%);
  cursor: ew-resize;
}

.chandle[data-dir="e"] { right: -7px; }
.chandle[data-dir="w"] { left: -7px; }

.chandle[data-dir="e"]::after,
.chandle[data-dir="w"]::after {
  top: 8px;
  bottom: 8px;
  left: 4px;
  right: 4px;
}

body.resizing { user-select: none; cursor: grabbing; }

.toast {
  position: fixed;
  bottom: 24px;
  right: 50%;
  transform: translateX(50%);
  background: #232838;
  border: 1px solid var(--accent);
  color: var(--text);
  padding: 10px 18px;
  border-radius: 10px;
  font-size: 13px;
  z-index: 200;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
}

.toast.error { border-color: #ff5c7a; }

::-webkit-scrollbar { width: 8px; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
::-webkit-scrollbar-track { background: transparent; }

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

  .app {
    display: flex;
    flex-direction: column;
    height: calc(100vh - var(--nav-h, 0px));
    height: calc(100dvh - var(--nav-h, 0px));
  }

  .preview-area {
    order: 1;
    flex: 1;
    min-height: 0;
    min-width: 0;
    overflow: hidden;
  }

  .preview-meta { display: none; }

  .preview-stage {
    padding: 12px;
    overflow: hidden;
  }

  .checker {
    max-width: 100%;
    max-height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  #preview-canvas {
    max-width: min(92vw, calc(100vh - var(--nav-h, 0px) - 300px));
    max-height: calc(100vh - var(--nav-h, 0px) - 300px);
    width: auto;
    height: auto;
  }

  .sidebar {
    order: 2;
    flex: 0 0 auto;
    min-width: 0;
    width: 100%;
    border-left: none;
    border-top: 1px solid var(--border);
    border-radius: 18px 18px 0 0;
    max-height: 50vh;
    max-height: 50dvh;
    padding: 0 12px calc(64px + env(safe-area-inset-bottom, 0px));
    box-shadow: 0 -12px 40px rgba(0, 0, 0, 0.5);
    overflow: hidden;
    display: flex;
    flex-direction: column;
    gap: 0;
  }

  .app-header { display: none; }

  .tabs {
    display: flex;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 100;
    background: var(--panel);
    border-top: 1px solid var(--border);
    padding: 8px 8px calc(8px + env(safe-area-inset-bottom, 0px));
    gap: 2px;
  }

  .tabs::before { display: none; }

  .tabs button {
    flex: 1 1 0;
    min-width: 0;
    background: transparent;
    border: none;
    color: var(--muted);
    font-family: inherit;
    font-size: 10.5px;
    cursor: pointer;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    padding: 8px 2px 4px;
    border-radius: 8px;
  }

  .tabs button .ic { font-size: 15px; }

  .tabs button.active {
    color: var(--accent);
    background: rgba(108, 140, 255, 0.1);
  }

  .sidebar .panel {
    display: none;
    animation: tabin 0.18s ease;
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    border: none;
    background: transparent;
    padding: 0;
  }

  .sidebar .panel.active { display: flex; }

  @keyframes tabin {
    from { opacity: 0; transform: translateY(6px); }
    to { opacity: 1; transform: none; }
  }

  .cpick-pop {
    right: auto;
    left: 0;
    width: min(280px, calc(100vw - 40px));
  }

  .cselect-menu { max-height: 220px; }
}

@media (min-width: 901px) {
  .sidebar .panel { display: flex !important; }
}
