/* ===== Reset & Base ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --pink:    #ff6b9d;
  --purple:  #a78bfa;
  --cyan:    #22d3ee;
  --yellow:  #fbbf24;
  --green:   #34d399;
  --blue:    #60a5fa;
  --accent:  #f472b6;
  --bg:      #0e0e1c;
  --surface: #16162a;
  --surface2:#1e1e36;
  --surface3:#252540;
  --border:  #2a2a4a;
  --text:    #e2e2f0;
  --dim:     #7878a0;
  --radius:  10px;
  --shadow:  0 4px 20px rgba(0,0,0,.5);
  --sidebar-w: 260px;
}

html, body { height: 100%; }

body {
  font-family: 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  font-size: 14px;
}

/* ===== Header ===== */
header {
  background: linear-gradient(135deg, #180a30 0%, #0e0e1c 100%);
  border-bottom: 1px solid var(--border);
  padding: 1rem 1.5rem;
  flex-shrink: 0;
}

.header-inner { max-width: 1600px; margin: 0 auto; }

.brand { display: flex; align-items: center; gap: .75rem; margin-bottom: .25rem; }

.brand-dots {
  display: flex; gap: 4px;
}
.brand-dots span {
  width: 10px; height: 10px; border-radius: 50%;
  display: inline-block;
  box-shadow: 0 0 6px currentColor;
}

.brand h1 {
  font-size: 1.4rem; font-weight: 700;
  background: linear-gradient(90deg, var(--pink), var(--purple), var(--cyan));
  -webkit-background-clip: text; -webkit-text-fill-color: transparent;
  background-clip: text;
}

.brand-sub { font-size: .78rem; color: var(--dim); letter-spacing: .05em; }

/* ===== App Layout ===== */
.app-layout {
  display: flex;
  flex: 1;
  overflow: hidden;
  height: calc(100vh - 72px);
}

/* ===== Sidebar ===== */
.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: .25rem;
  padding: .75rem .6rem;
}

.sidebar::-webkit-scrollbar { width: 4px; }
.sidebar::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

/* ===== Panel ===== */
.panel {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .75rem;
  display: flex;
  flex-direction: column;
  gap: .5rem;
}

.panel-title {
  font-size: .75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--dim);
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: .25rem;
}

/* ===== Upload Zone ===== */
.upload-zone {
  border: 2px dashed var(--border);
  border-radius: 8px;
  padding: 1.2rem .75rem;
  text-align: center;
  cursor: pointer;
  transition: border-color .2s, background .2s;
}

.upload-zone:hover, .upload-zone.drag-over {
  border-color: var(--purple);
  background: rgba(167,139,250,.06);
}

.upload-icon svg { width: 40px; height: 40px; margin-bottom: .4rem; }

.upload-zone p { font-size: .82rem; color: var(--dim); line-height: 1.6; }
.upload-zone .upload-hint { font-size: .72rem; color: #4a4a70; }

/* ===== Params ===== */
.param-row {
  display: flex;
  flex-direction: column;
  gap: .25rem;
}

.param-row label {
  font-size: .78rem;
  color: var(--dim);
  display: flex;
  align-items: center;
  gap: .35rem;
}

.val-badge {
  background: linear-gradient(135deg, var(--pink), var(--purple));
  color: #fff;
  font-size: .68rem;
  padding: .05rem .35rem;
  border-radius: 999px;
  font-weight: 700;
  min-width: 26px;
  text-align: center;
  display: inline-block;
}

input[type="range"] {
  width: 100%; height: 4px; cursor: pointer;
  accent-color: var(--purple); border-radius: 2px;
}

.toggle-row {
  display: flex; flex-direction: column; gap: .3rem;
}
.toggle-row label {
  display: flex; align-items: center; gap: .4rem;
  font-size: .8rem; cursor: pointer; color: var(--text);
}
.toggle-row input[type="checkbox"] { accent-color: var(--purple); width: 14px; height: 14px; cursor: pointer; }

/* ===== Buttons ===== */
.btn {
  padding: .45rem 1rem;
  border: none; border-radius: 7px;
  font-size: .82rem; font-weight: 600;
  cursor: pointer;
  transition: opacity .15s, transform .1s;
  display: inline-flex; align-items: center; justify-content: center;
}
.btn:hover { opacity: .85; }
.btn:active { transform: scale(.97); }

.btn-primary { background: linear-gradient(135deg, var(--pink), var(--purple)); color: #fff; }
.btn-accent  { background: linear-gradient(135deg, var(--yellow), var(--green)); color: #111; }
.btn-ghost   { background: var(--surface3); color: var(--dim); border: 1px solid var(--border); }
.btn-sm      { padding: .3rem .8rem; font-size: .78rem; background: var(--surface3); color: var(--text); border: 1px solid var(--border); }
.btn-sm:hover { background: var(--border); }
.w-full      { width: 100%; }

/* ===== Palette List ===== */
.palette-list {
  display: flex;
  flex-direction: column;
  gap: .3rem;
  max-height: 260px;
  overflow-y: auto;
}
.palette-list::-webkit-scrollbar { width: 3px; }
.palette-list::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.palette-item {
  display: flex;
  align-items: center;
  gap: .5rem;
  padding: .35rem .5rem;
  border-radius: 6px;
  cursor: pointer;
  transition: background .15s;
  border: 1px solid transparent;
}
.palette-item:hover { background: var(--surface3); border-color: var(--border); }
.palette-item.active { border-color: var(--purple); background: rgba(167,139,250,.1); }

.pal-swatch {
  width: 28px; height: 28px; border-radius: 5px;
  flex-shrink: 0;
  border: 1px solid rgba(255,255,255,.1);
  box-shadow: 0 1px 6px rgba(0,0,0,.3);
}

.pal-info { flex: 1; min-width: 0; }
.pal-hex  { font-size: .72rem; font-family: monospace; color: var(--text); }
.pal-rgb  { font-size: .65rem; color: var(--dim); }
.pal-count{ font-size: .65rem; color: var(--dim); text-align: right; flex-shrink: 0; }

.count-tag {
  font-size: .68rem; background: var(--surface3);
  border: 1px solid var(--border);
  color: var(--dim);
  padding: .05rem .4rem; border-radius: 999px;
}

/* ===== Edit Panel ===== */
.edit-panel { border-color: var(--purple); }

.edit-info {
  font-size: .75rem; color: var(--dim);
  background: var(--surface3);
  border-radius: 6px;
  padding: .4rem .6rem;
  line-height: 1.7;
}

.slider-group {
  display: flex;
  align-items: center;
  gap: .4rem;
  flex-wrap: wrap;
}

.slider-group label {
  width: 28px;
  font-size: .8rem; font-weight: 700;
  display: flex; align-items: center; gap: .25rem;
}

.slider-group input[type="range"] {
  flex: 1; min-width: 80px;
}

.slider-r { accent-color: #ff4444; }
.slider-g { accent-color: #44cc44; }
.slider-b { accent-color: #4488ff; }

.num-input {
  width: 44px; background: var(--surface3);
  border: 1px solid var(--border); border-radius: 5px;
  color: var(--text); font-size: .75rem;
  padding: .2rem .3rem; text-align: center;
  outline: none;
}
.num-input:focus { border-color: var(--purple); }

.edit-preview {
  display: flex; align-items: center; gap: .6rem;
  padding: .4rem 0;
}

.preview-box {
  width: 44px; height: 44px;
  border-radius: 8px;
  border: 2px solid rgba(255,255,255,.15);
  box-shadow: 0 2px 10px rgba(0,0,0,.4);
  flex-shrink: 0;
}

.preview-hex {
  font-family: monospace;
  font-size: .9rem;
  font-weight: 700;
  color: var(--text);
}

.edit-actions {
  display: flex; gap: .4rem;
}
.edit-actions .btn { flex: 1; font-size: .75rem; }

/* ===== Canvas Area ===== */
.canvas-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
}

.canvas-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: .5rem 1rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  gap: .75rem;
  flex-wrap: wrap;
}

.toolbar-left { font-size: .75rem; color: var(--dim); }
.toolbar-right { display: flex; gap: .5rem; }

.canvas-scroll {
  flex: 1;
  overflow: auto;
  padding: 1rem;
  background: var(--bg);
  display: flex;
  align-items: flex-start;
  justify-content: flex-start;
}

.canvas-scroll::-webkit-scrollbar { width: 8px; height: 8px; }
.canvas-scroll::-webkit-scrollbar-track { background: var(--bg); }
.canvas-scroll::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

#gridCanvas {
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  cursor: crosshair;
  display: block;
  box-shadow: 0 0 0 1px var(--border), var(--shadow);
}

/* ===== Welcome ===== */
.welcome-msg {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: .75rem;
  color: var(--dim);
  font-size: .95rem;
  line-height: 1.8;
  text-align: center;
}

.welcome-icon {
  display: flex; gap: 8px; margin-bottom: .5rem;
}
.welcome-icon span {
  width: 20px; height: 20px; border-radius: 50%;
  display: inline-block;
  animation: bounce 1.4s ease-in-out infinite;
}
.welcome-icon span:nth-child(2) { animation-delay: .15s; }
.welcome-icon span:nth-child(3) { animation-delay: .3s; }
.welcome-icon span:nth-child(4) { animation-delay: .45s; }
.welcome-icon span:nth-child(5) { animation-delay: .6s; }
.welcome-icon span:nth-child(6) { animation-delay: .75s; }

@keyframes bounce {
  0%, 100% { transform: translateY(0); }
  50%       { transform: translateY(-10px); }
}

/* ===== Stats Section ===== */
.stats-section {
  border-top: 1px solid var(--border);
  background: var(--surface);
  flex-shrink: 0;
  max-height: 300px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.stats-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: .6rem 1rem;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.stats-title {
  font-size: .85rem; font-weight: 700;
  text-transform: uppercase; letter-spacing: .07em;
  color: var(--dim);
}

.stats-summary { font-size: .78rem; color: var(--dim); }

.table-wrap {
  overflow: auto; flex: 1;
}
.table-wrap::-webkit-scrollbar { width: 5px; height: 5px; }
.table-wrap::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

.stats-table {
  width: 100%;
  border-collapse: collapse;
  font-size: .78rem;
}

.stats-table th, .stats-table td {
  padding: .4rem .75rem;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  text-align: left;
}

.stats-table th {
  background: var(--surface2);
  color: var(--dim);
  font-weight: 600;
  text-transform: uppercase;
  font-size: .7rem;
  letter-spacing: .05em;
  position: sticky; top: 0; z-index: 1;
}

.stats-table th.sortable { cursor: pointer; user-select: none; }
.stats-table th.sortable:hover { color: var(--text); }
.stats-table th.sort-asc::after  { content: ' ▲'; color: var(--purple); }
.stats-table th.sort-desc::after { content: ' ▼'; color: var(--purple); }

.stats-table tr:hover td { background: rgba(255,255,255,.03); }

.tbl-swatch {
  width: 24px; height: 24px; border-radius: 5px;
  border: 1px solid rgba(255,255,255,.1);
  display: inline-block;
  vertical-align: middle;
}

.tbl-hex { font-family: monospace; font-weight: 600; }

.pct-bar-wrap {
  display: flex; align-items: center; gap: .4rem;
}
.pct-bar {
  height: 6px; border-radius: 3px;
  display: inline-block; min-width: 2px;
}
.pct-text { font-size: .72rem; color: var(--dim); }

/* ===== Mard Color Picker ===== */
.mard-search {
  width: 100%;
  background: var(--surface3);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-size: .78rem;
  padding: .35rem .6rem;
  outline: none;
}
.mard-search:focus { border-color: var(--purple); }

.mard-picker-wrap {
  max-height: 200px;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: .4rem;
  background: var(--surface3);
}
.mard-picker-wrap::-webkit-scrollbar { width: 4px; }
.mard-picker-wrap::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.mard-picker-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 3px;
}

.picker-swatch {
  width: 22px; height: 22px;
  border-radius: 4px;
  border: 1px solid rgba(255,255,255,.1);
  cursor: pointer;
  transition: transform .1s, border-color .1s;
  position: relative;
}
.picker-swatch:hover { transform: scale(1.2); z-index: 1; border-color: rgba(255,255,255,.5); }
.picker-swatch.selected {
  border: 2px solid #fff;
  box-shadow: 0 0 0 2px var(--purple);
  transform: scale(1.15);
  z-index: 2;
}

.picked-preview {
  font-size: .75rem;
  font-family: monospace;
  background: var(--surface3);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: .35rem .6rem;
  color: var(--text);
  word-break: break-all;
  min-height: 28px;
  line-height: 1.6;
}

.mard-tip {
  font-size: .78rem;
  color: var(--purple);
  margin-top: .25rem;
}

/* ===== Cell Tooltip ===== */
.cell-tooltip {
  position: fixed;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: .5rem .8rem;
  font-size: .75rem;
  pointer-events: none;
  z-index: 999;
  box-shadow: var(--shadow);
  line-height: 1.8;
  max-width: 220px;
}

.tooltip-swatch {
  width: 14px; height: 14px;
  border-radius: 3px;
  display: inline-block;
  vertical-align: middle;
  margin-right: 5px;
  border: 1px solid rgba(255,255,255,.2);
}

/* ===== Toast ===== */
.toast {
  position: fixed; bottom: 1.5rem; left: 50%;
  transform: translateX(-50%) translateY(16px);
  background: linear-gradient(135deg, var(--pink), var(--purple));
  color: #fff; padding: .5rem 1.2rem;
  border-radius: 999px; font-size: .82rem; font-weight: 600;
  opacity: 0; pointer-events: none;
  transition: opacity .22s, transform .22s;
  z-index: 9999; white-space: nowrap;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
