/* ============================================================
   eSign — styles
   Light/dark themes via CSS variables, responsive, accessible.
   ============================================================ */

/* ---------- Design tokens ---------- */
:root {
  --brand: #4f46e5;
  --brand-600: #4338ca;
  --brand-700: #3730a3;
  --brand-soft: rgba(79, 70, 229, 0.12);

  /* Light theme */
  --bg: #f4f5f9;
  --bg-elev: #ffffff;
  --surface: #ffffff;
  --surface-2: #f1f3f9;
  --border: #e2e6f0;
  --text: #0f172a;
  --text-muted: #5a6478;
  --text-invert: #ffffff;
  --canvas-bg: #ffffff;
  --canvas-grid: rgba(15, 23, 42, 0.04);
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.06);
  --shadow-md: 0 4px 14px rgba(15, 23, 42, 0.08);
  --shadow-lg: 0 12px 32px rgba(15, 23, 42, 0.12);
  --radius: 12px;
  --radius-sm: 8px;
  --radius-lg: 16px;
  --transition: 160ms cubic-bezier(0.4, 0, 0.2, 1);
  --ring: 0 0 0 3px var(--brand-soft);
}

@media (prefers-color-scheme: dark) {
  :root {
    --brand: #818cf8;
    --brand-600: #6366f1;
    --brand-700: #4f46e5;
    --brand-soft: rgba(129, 140, 248, 0.18);

    --bg: #0b1020;
    --bg-elev: #131a2c;
    --surface: #161e33;
    --surface-2: #1c2540;
    --border: #2a3454;
    --text: #e8edf7;
    --text-muted: #9aa6c2;
    --text-invert: #0f172a;
    --canvas-bg: #ffffff;
    --canvas-grid: rgba(15, 23, 42, 0.04);
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
    --shadow-md: 0 4px 14px rgba(0, 0, 0, 0.45);
    --shadow-lg: 0 12px 32px rgba(0, 0, 0, 0.55);
  }
}

/* Manual override classes set on <html> */
html[data-theme="light"] {
  --brand: #4f46e5;
  --brand-600: #4338ca;
  --brand-700: #3730a3;
  --brand-soft: rgba(79, 70, 229, 0.12);
  --bg: #f4f5f9;
  --bg-elev: #ffffff;
  --surface: #ffffff;
  --surface-2: #f1f3f9;
  --border: #e2e6f0;
  --text: #0f172a;
  --text-muted: #5a6478;
  --text-invert: #ffffff;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.06);
  --shadow-md: 0 4px 14px rgba(15, 23, 42, 0.08);
  --shadow-lg: 0 12px 32px rgba(15, 23, 42, 0.12);
}

html[data-theme="dark"] {
  --brand: #818cf8;
  --brand-600: #6366f1;
  --brand-700: #4f46e5;
  --brand-soft: rgba(129, 140, 248, 0.18);
  --bg: #0b1020;
  --bg-elev: #131a2c;
  --surface: #161e33;
  --surface-2: #1c2540;
  --border: #2a3454;
  --text: #e8edf7;
  --text-muted: #9aa6c2;
  --text-invert: #0f172a;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
  --shadow-md: 0 4px 14px rgba(0, 0, 0, 0.45);
  --shadow-lg: 0 12px 32px rgba(0, 0, 0, 0.55);
}

/* ---------- Reset ---------- */
*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  transition: background var(--transition), color var(--transition);
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}
img { display: block; max-width: 100%; }
button { font: inherit; color: inherit; cursor: pointer; }
input { font: inherit; }
:focus-visible {
  outline: none;
  box-shadow: var(--ring);
  border-radius: var(--radius-sm);
}

.sr-only {
  position: absolute; width: 1px; height: 1px;
  padding: 0; margin: -1px; overflow: hidden;
  clip: rect(0,0,0,0); white-space: nowrap; border: 0;
}
.skip-link {
  position: absolute; top: -100px; left: 0;
  background: var(--brand); color: var(--text-invert);
  padding: 10px 16px; border-radius: 0 0 var(--radius-sm) 0;
  z-index: 100; text-decoration: none;
  transition: top var(--transition);
}
.skip-link:focus { top: 0; }

/* ---------- Header ---------- */
.app-header {
  position: sticky; top: 0; z-index: 20;
  background: color-mix(in srgb, var(--bg-elev) 85%, transparent);
  backdrop-filter: saturate(140%) blur(10px);
  border-bottom: 1px solid var(--border);
}
.app-header__inner {
  max-width: 1100px; margin: 0 auto;
  padding: 14px 20px;
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand__logo { border-radius: 9px; box-shadow: var(--shadow-sm); }
.brand__name {
  font-size: 1.4rem; font-weight: 700; margin: 0;
  letter-spacing: -0.02em; color: var(--text);
}
.brand__tagline { margin: 0; font-size: 0.8rem; color: var(--text-muted); }

.icon-btn {
  width: 42px; height: 42px; border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface); color: var(--text);
  display: grid; place-items: center;
  font-size: 1.1rem; line-height: 1;
  transition: background var(--transition), border-color var(--transition), transform var(--transition);
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn:active { transform: scale(0.95); }
.icon-btn__sun, .icon-btn__moon { transition: opacity var(--transition); }
html[data-theme="dark"] .icon-btn__sun,
@media (prefers-color-scheme: dark) { html:not([data-theme]) .icon-btn__sun { opacity: 0; } }
html[data-theme="light"] .icon-btn__moon { opacity: 0; }
html:not([data-theme]) .icon-btn__moon {
  opacity: 0;
}
@media (prefers-color-scheme: dark) {
  html:not([data-theme]) .icon-btn__moon { opacity: 1; }
  html:not([data-theme]) .icon-btn__sun { opacity: 0; }
}

/* ---------- Layout ---------- */
.app-main {
  flex: 1 0 auto;
  width: 100%;
  max-width: 1100px;
  margin: 0 auto;
  padding: 24px 20px 40px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

/* ---------- Canvas ---------- */
.canvas-section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  padding: 16px;
}
.canvas-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 3 / 1;
  background:
    linear-gradient(var(--canvas-grid) 1px, transparent 1px) 0 0 / 24px 24px,
    linear-gradient(90deg, var(--canvas-grid) 1px, transparent 1px) 0 0 / 24px 24px,
    var(--canvas-bg);
  border-radius: var(--radius);
  overflow: hidden;
  touch-action: none;
  user-select: none;
}
.signature-canvas {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  display: block;
  touch-action: none;
  cursor: crosshair;
}
.canvas-wrap[data-mode="type"] .signature-canvas { cursor: grab; }
.canvas-wrap[data-mode="type"] .signature-canvas.dragging { cursor: grabbing; }
.signature-canvas:focus-visible { box-shadow: inset 0 0 0 3px var(--brand); }

.canvas-hint {
  position: absolute;
  left: 50%; top: 50%;
  transform: translate(-50%, -50%);
  margin: 0;
  color: #94a0b8;
  font-size: 0.95rem;
  pointer-events: none;
  transition: opacity var(--transition);
  user-select: none;
}
.canvas-hint.is-hidden { opacity: 0; }

/* ---------- Tools ---------- */
.tools {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.tabs { display: flex; gap: 6px; }
.tab {
  flex: 1;
  padding: 10px 14px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-muted);
  border-radius: var(--radius-sm);
  font-weight: 600;
  transition: all var(--transition);
}
.tab:hover { color: var(--text); }
.tab--active {
  background: var(--brand);
  color: var(--text-invert);
  border-color: var(--brand);
}

.panel { display: none; flex-direction: column; gap: 14px; }
.panel--active { display: flex; }

.tool-group {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
}
.tool-group--stack { flex-direction: column; align-items: stretch; }
.tool-group__label {
  font-size: 0.8rem; font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase; letter-spacing: 0.04em;
  min-width: 72px;
}
.tool-group__value {
  display: inline-block;
  min-width: 1.6em; text-align: center;
  color: var(--brand);
  font-weight: 700;
}

/* Color swatches */
.colors { display: flex; gap: 8px; }
.swatch {
  width: 30px; height: 30px;
  border-radius: 50%;
  background: var(--swatch);
  border: 2px solid transparent;
  box-shadow: inset 0 0 0 1px rgba(0,0,0,0.12), var(--shadow-sm);
  transition: transform var(--transition), border-color var(--transition);
  position: relative;
}
.swatch:hover { transform: scale(1.1); }
.swatch.is-active {
  border-color: var(--brand);
  transform: scale(1.1);
}
.swatch.is-active::after {
  content: ""; position: absolute; inset: -5px;
  border: 1px solid var(--brand);
  border-radius: 50%;
}
.custom-color input[type="color"] {
  width: 38px; height: 38px;
  padding: 0; border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  cursor: pointer;
}
.custom-color input[type="color"]::-webkit-color-swatch-wrapper { padding: 2px; }
.custom-color input[type="color"]::-webkit-color-swatch { border: none; border-radius: 4px; }

/* Segmented controls */
.segmented {
  display: inline-flex;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 3px;
  gap: 2px;
}
.seg {
  padding: 6px 14px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  border-radius: 5px;
  font-size: 0.85rem; font-weight: 600;
  transition: all var(--transition);
}
.seg:hover { color: var(--text); }
.seg.is-active {
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-sm);
}

/* Range */
.range {
  -webkit-appearance: none; appearance: none;
  flex: 1; min-width: 120px;
  height: 6px; border-radius: 3px;
  background: var(--surface-2);
  outline: none;
}
.range::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none;
  width: 20px; height: 20px;
  border-radius: 50%;
  background: var(--brand);
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: transform var(--transition);
}
.range::-webkit-slider-thumb:hover { transform: scale(1.15); }
.range::-moz-range-thumb {
  width: 20px; height: 20px;
  border-radius: 50%;
  background: var(--brand);
  cursor: pointer; border: none;
}

/* Text input */
.text-input {
  width: 100%;
  padding: 11px 14px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  border-radius: var(--radius-sm);
  font-size: 1rem;
  transition: border-color var(--transition), box-shadow var(--transition);
}
.text-input:focus {
  border-color: var(--brand);
  box-shadow: var(--ring);
}

.checkbox {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 0.85rem; color: var(--text-muted); font-weight: 600;
  cursor: pointer;
}
.checkbox input { accent-color: var(--brand); width: 16px; height: 16px; }

.hint { font-size: 0.8rem; color: var(--text-muted); margin: 0; }

/* ---------- Actions ---------- */
.actions {
  display: flex; flex-wrap: wrap; gap: 10px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
}
.btn {
  display: inline-flex; align-items: center; gap: 7px;
  padding: 10px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font-weight: 600; font-size: 0.9rem;
  transition: all var(--transition);
  white-space: nowrap;
}
.btn:hover { transform: translateY(-1px); box-shadow: var(--shadow-sm); }
.btn:active { transform: translateY(0) scale(0.98); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; transform: none; box-shadow: none; }
.btn--ghost { background: var(--surface-2); }
.btn--primary {
  background: var(--brand);
  color: var(--text-invert);
  border-color: var(--brand);
}
.btn--primary:hover { background: var(--brand-600); border-color: var(--brand-600); }
.btn--secondary {
  background: var(--brand-soft);
  color: var(--brand);
  border-color: transparent;
}

/* ---------- Shortcuts ---------- */
.shortcuts {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 14px 18px;
  box-shadow: var(--shadow-sm);
}
.shortcuts__title { margin: 0 0 8px; font-size: 0.8rem; text-transform: uppercase;
  letter-spacing: 0.04em; color: var(--text-muted); }
.shortcuts__list {
  margin: 0; padding: 0; list-style: none;
  display: flex; flex-wrap: wrap; gap: 8px 20px;
  font-size: 0.85rem; color: var(--text-muted);
}
kbd {
  display: inline-block;
  padding: 2px 7px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 5px;
  font-family: ui-monospace, "SFMono-Regular", Menlo, monospace;
  font-size: 0.78rem;
  color: var(--text);
}

/* ---------- Footer ---------- */
.app-footer {
  text-align: center;
  padding: 18px 20px 28px;
  color: var(--text-muted);
  font-size: 0.82rem;
}

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  bottom: 24px; left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--text);
  color: var(--bg);
  padding: 12px 20px;
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  font-size: 0.9rem; font-weight: 500;
  opacity: 0;
  transition: opacity var(--transition), transform var(--transition);
  z-index: 1000;
  pointer-events: none;
  max-width: calc(100% - 40px);
}
.toast.is-visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}
.toast--error { background: #b91c1c; color: #fff; }
.toast--success { background: #047857; color: #fff; }

/* ---------- Responsive ---------- */
@media (max-width: 640px) {
  .app-header__inner { padding: 12px 16px; }
  .brand__tagline { display: none; }
  .app-main { padding: 16px 12px 32px; }
  .canvas-wrap { aspect-ratio: 2 / 1; }
  .tool-group__label { width: 100%; min-width: 0; }
  .tool-group { padding-bottom: 4px; border-bottom: 1px dashed var(--border); }
  .actions .btn span { display: none; }
  .actions .btn { padding: 11px 13px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
  }
}
