:root {
  --danger: #c62828;
  --radius: 16px;
  --radius-sm: 10px;
  --radius-lg: 24px;
  --font: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --font-mono: ui-monospace, Menlo, Consolas, monospace;
  --transition: 180ms cubic-bezier(0.2, 0, 0, 1);}

body.dark {
  --danger: #ef9a9a;}

*, *::before, *::after { box-sizing: border-box; }
* { -webkit-tap-highlight-color: transparent; }

html, body { margin: 0; padding: 0; }

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  padding: 20px;
  min-height: 100vh;
  transition: background 0.25s, color 0.25s;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

.container {
  max-width: 920px;
  margin: 0 auto;
  background: var(--card);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  padding: 24px 32px 32px;
}

.top-nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.4rem;
  gap: 0.75rem;
}

.header-controls { display: flex; gap: 0.45rem; align-items: center; }

.page-title {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  font-size: 1.9rem;
  font-weight: 700;
  letter-spacing: -0.3px;
  text-align: center;
  margin: 0 0 22px;
  color: var(--text);
}
.page-title svg { width: 24px; height: 24px; flex-shrink: 0; }

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 8px 12px;
  background: color-mix(in srgb, var(--bg) 65%, var(--card));
  border: 1px solid var(--border);
  border-radius: var(--radius);
  align-items: center;
  margin-bottom: 12px;
}

.toolbar-group {
  display: flex;
  align-items: center;
  gap: 2px;
  padding-right: 6px;
  border-right: 1px solid var(--border);
  height: 36px;
}
.toolbar-group:last-child { border-right: none; padding-right: 0; }
.toolbar-spacer { flex: 1; min-width: 10px; }

.toolbar-btn {
  min-width: 36px;
  height: 36px;
  padding: 0;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: transparent;
  cursor: pointer;
  color: var(--text);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
  touch-action: manipulation;
  flex-shrink: 0;
}
.toolbar-btn svg { width: 18px; height: 18px; pointer-events: none; }
.toolbar-btn:hover,
.toolbar-btn.active {
  background: var(--accent-soft);
  border-color: color-mix(in srgb, var(--accent) 35%, var(--border));
}
.toolbar-btn:active { transform: scale(0.94); }

.font-size-picker { position: relative; }
.font-size-btn {
  min-width: 64px;
  height: 36px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--card);
  color: var(--text);
  font-family: var(--font);
  font-size: 0.85rem;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  justify-content: space-between;
  gap: 4px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}
.font-size-btn svg { width: 12px; height: 12px; opacity: 0.6; transition: transform 0.18s; }
.font-size-btn[aria-expanded="true"] { background: var(--accent-soft); border-color: var(--accent); }
.font-size-btn[aria-expanded="true"] svg { transform: rotate(180deg); }

.font-size-menu {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  z-index: 50;
  min-width: 72px;
  margin: 0;
  padding: 6px;
  list-style: none;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow-elevated);
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.font-size-menu[hidden] { display: none; }
.font-size-menu li {
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--text);
  cursor: pointer;
  text-align: center;
}
.font-size-menu li:hover { background: var(--accent-soft); }
.font-size-menu li.active { background: var(--accent); color: #fff; }
body.dark .font-size-menu li.active { color: #0d1a12; }

.color-btn { position: relative; }
.color-indicator {
  position: absolute;
  bottom: 4px;
  left: 50%;
  transform: translateX(-50%);
  width: 14px;
  height: 4px;
  border-radius: 2px;
  background: var(--text);
  box-shadow: 0 0 0 1px var(--card);
  pointer-events: none;
}

.editor-wrapper {
  background: color-mix(in srgb, var(--bg) 50%, var(--card));
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  margin-bottom: 16px;
}

.editor {
  min-height: 340px;
  max-height: 58vh;
  overflow-y: auto;
  padding: 20px 22px;
  outline: none;
  font-size: 16px;
  line-height: 1.65;
  color: var(--text);
}
.editor:empty:before {
  content: attr(data-placeholder);
  color: var(--muted);
  opacity: 0.7;
  pointer-events: none;
}
.editor table { border-collapse: collapse; width: 100%; margin: 12px 0; }
.editor table td, .editor table th { border: 1px solid var(--border); padding: 8px 12px; min-width: 60px; }
.editor table th { background: color-mix(in srgb, var(--accent-soft) 60%, transparent); font-weight: 650; }
.editor ul, .editor ol { padding-left: 28px; margin: 8px 0; }
.editor p { margin: 0 0 0.55em; }

.media-wrap {
  position: relative;
  display: inline-block;
  max-width: 100%;
  vertical-align: top;
  margin: 6px 4px;
  user-select: none;
}
.media-wrap.selected { outline: 2px solid var(--accent); outline-offset: 2px; }
.media-wrap img {
  display: block; width: 100%; height: 100%; object-fit: contain;
  border-radius: 4px; pointer-events: none;
}
.media-wrap .shape-svg {
  display: block; width: 100%; height: 100%; pointer-events: none;
}

.media-handle {
  position: absolute; right: -7px; bottom: -7px;
  width: 16px; height: 16px;
  background: var(--accent); border: 2px solid var(--card);
  border-radius: 4px; cursor: nwse-resize; touch-action: none;
  display: none; z-index: 3;
}
.media-wrap.selected .media-handle { display: block; }

.media-delete {
  position: absolute; top: -10px; right: -10px;
  width: 22px; height: 22px;
  border-radius: 50%; border: 1px solid var(--border);
  background: var(--card); color: var(--danger);
  cursor: pointer; display: none; align-items: center; justify-content: center;
  padding: 0; z-index: 3;
}
.media-delete svg { width: 12px; height: 12px; }
.media-wrap.selected .media-delete { display: flex; }
.media-delete:hover { background: var(--danger); color: #fff; }

@media (pointer: coarse) {
  .media-handle { width: 22px; height: 22px; right: -11px; bottom: -11px; }
  .media-delete { width: 28px; height: 28px; top: -14px; right: -14px; }
}

.actions-row {
  display: flex; flex-wrap: wrap; gap: 10px;
  align-items: center; justify-content: space-between;
  margin-bottom: 8px;
}
.actions-group { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.export-label { font-size: 0.85rem; font-weight: 600; color: var(--muted); }

.action-btn {
  background: var(--accent-soft); border: 1px solid transparent;
  padding: 8px 14px; border-radius: 30px;
  font-size: 0.85rem; font-weight: 600;
  cursor: pointer; color: var(--text);
  display: inline-flex; align-items: center; gap: 6px;
  font-family: var(--font); transition: transform 0.12s, filter 0.15s;
  touch-action: manipulation;
}
.action-btn svg { width: 15px; height: 15px; }
.action-btn:hover { filter: brightness(0.97); }
.action-btn:active { transform: scale(0.96); }
.action-btn.primary { background: var(--accent); color: #fff; }
body.dark .action-btn.primary { color: #0d1a12; }
.action-btn:disabled { opacity: 0.45; cursor: not-allowed; filter: none; transform: none; }
.action-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.modal {
  position: fixed; inset: 0; z-index: 1000;
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
}
.modal[hidden] { display: none; }
body.modal-open { overflow: hidden; }

.modal-backdrop { position: absolute; inset: 0; background: rgba(15, 23, 42, 0.5); }
body.dark .modal-backdrop { background: rgba(0, 0, 0, 0.7); }

.modal-card {
  position: relative; background: var(--card);
  border-radius: var(--radius-lg); box-shadow: var(--shadow-elevated);
  width: 100%; max-width: 360px; overflow: hidden; z-index: 1;
}

.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 18px; border-bottom: 1px solid var(--border);
}
.modal-head h3 { font-size: 1.05rem; font-weight: 700; margin: 0; }

.modal-close {
  width: 34px; height: 34px; border: 1px solid var(--border);
  border-radius: 50%; background: var(--card); color: var(--muted);
  cursor: pointer; display: inline-flex; align-items: center; justify-content: center;
  transition: background 0.15s, color 0.15s;
}
.modal-close svg { width: 16px; height: 16px; }
.modal-close:hover { background: var(--accent); color: #fff; border-color: var(--accent); }

.color-picker-card { max-width: 320px; }
.color-picker { padding: 16px 18px 18px; display: flex; flex-direction: column; gap: 14px; }

.sv-area {
  position: relative; width: 100%; aspect-ratio: 4 / 3;
  border-radius: 10px; overflow: hidden; cursor: crosshair; touch-action: none;
}
.sv-gradient {
  position: absolute; inset: 0;
  background: linear-gradient(to top, #000, transparent), linear-gradient(to right, #fff, hsl(var(--hue, 0), 100%, 50%));
}
.sv-handle {
  position: absolute; width: 14px; height: 14px; 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; left: 100%; top: 0;
}

.hue-range {
  -webkit-appearance: none; appearance: none; width: 100%; height: 16px;
  border-radius: 8px; outline: none; cursor: pointer; touch-action: manipulation;
  background: linear-gradient(to right, hsl(0, 100%, 50%), hsl(60, 100%, 50%), hsl(120, 100%, 50%), hsl(180, 100%, 50%), hsl(240, 100%, 50%), hsl(300, 100%, 50%), hsl(360, 100%, 50%));
}
.hue-range::-webkit-slider-thumb {
  -webkit-appearance: none; width: 22px; height: 22px; border-radius: 50%;
  background: #fff; border: 2px solid var(--text); box-shadow: 0 1px 4px rgba(0,0,0,0.3); cursor: pointer;
}
.hue-range::-moz-range-thumb {
  width: 22px; height: 22px; border-radius: 50%;
  background: #fff; border: 2px solid var(--text); box-shadow: 0 1px 4px rgba(0,0,0,0.3); cursor: pointer;
}

.color-inputs { display: flex; gap: 8px; align-items: center; }
.color-preview { width: 40px; height: 40px; border-radius: 10px; border: 1px solid var(--border); flex-shrink: 0; background: #000; }
.hex-input {
  flex: 1; min-width: 0; height: 40px; padding: 0 12px;
  border: 1px solid var(--border); border-radius: 10px;
  font-family: var(--font-mono); font-size: 0.9rem; font-weight: 500;
  background: var(--card); color: var(--text); text-transform: uppercase; letter-spacing: 0.5px;
}
.hex-input:focus { outline: none; border-color: var(--accent); }

.apply-color-btn {
  width: 100%; height: 42px; border: none; border-radius: 10px;
  background: var(--accent); color: #fff; font-family: var(--font);
  font-weight: 700; font-size: 0.9rem; cursor: pointer;
  transition: filter 0.15s, transform 0.1s;
}
body.dark .apply-color-btn { color: #0d1a12; }
.apply-color-btn:hover { filter: brightness(1.08); }
.apply-color-btn:active { transform: scale(0.97); }

.shape-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; padding: 16px 18px 18px; }
.shape-option {
  aspect-ratio: 1; border: 1px solid var(--border); border-radius: 12px;
  background: var(--card); color: var(--text); cursor: pointer;
  display: flex; align-items: center; justify-content: center; padding: 12px;
  transition: background 0.15s, border-color 0.15s, transform 0.1s; touch-action: manipulation;
}
.shape-option svg { width: 100%; height: 100%; }
.shape-option:hover { background: var(--accent-soft); border-color: var(--accent); }
.shape-option:active { transform: scale(0.95); }

.calc-card { max-width: 300px; }
.calc { padding: 16px; }
.calc-display {
  width: 100%; height: 56px; font-size: 1.6rem; text-align: right; padding: 0 14px;
  border: 1px solid var(--border); border-radius: 12px; margin-bottom: 12px;
  background: color-mix(in srgb, var(--bg) 70%, var(--card)); color: var(--text); font-family: var(--font-mono);
}
.calc-buttons { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.calc-btn {
  height: 50px; border: 1px solid var(--border); border-radius: 12px;
  background: var(--card); color: var(--text); font-size: 1.15rem;
  font-family: var(--font); font-weight: 500; cursor: pointer;
  transition: background 0.12s, transform 0.1s; touch-action: manipulation;
}
.calc-btn:hover { background: var(--accent-soft); }
.calc-btn:active { transform: scale(0.94); }
.calc-btn.op { background: var(--accent-soft); color: var(--accent); font-weight: 650; }
.calc-btn.eq { background: var(--accent); color: #fff; border-color: var(--accent); }
body.dark .calc-btn.eq { color: #0d1a12; }
.calc-btn.zero { grid-column: span 2; }

.tool-card { max-width: 440px; }
.tool { padding: 16px 18px 18px; display: flex; flex-direction: column; gap: 14px; }
.tool-scope { margin: 0; font-size: 0.85rem; font-weight: 600; color: var(--muted); }

.segmented {
  display: flex; gap: 4px; padding: 4px;
  background: color-mix(in srgb, var(--bg) 65%, var(--card));
  border: 1px solid var(--border); border-radius: var(--radius-sm);
}
.segmented-btn {
  flex: 1; min-height: 36px; padding: 0 10px;
  border: 0; border-radius: 8px; background: transparent; color: var(--text);
  font-family: var(--font); font-size: 0.85rem; font-weight: 600;
  cursor: pointer; transition: background 0.15s, color 0.15s; touch-action: manipulation;
}
.segmented-btn:hover { background: var(--accent-soft); }
.segmented-btn.active { background: var(--accent); color: #fff; }
body.dark .segmented-btn.active { color: #0d1a12; }
.segmented-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.tool-preview { display: flex; flex-direction: column; gap: 8px; }
.tool-preview-row {
  padding: 10px 12px; border: 1px solid var(--border); border-radius: 12px;
  background: color-mix(in srgb, var(--bg) 50%, var(--card));
}
.tool-preview-row.is-after { border-color: color-mix(in srgb, var(--accent) 45%, var(--border)); }
.tool-preview-label {
  display: block; margin-bottom: 4px; font-size: 0.72rem; font-weight: 700;
  letter-spacing: 0.04em; text-transform: uppercase; color: var(--muted);
}
.tool-preview-text {
  margin: 0; min-height: 1.5em; font-size: 0.95rem; line-height: 1.5;
  white-space: pre-wrap; overflow-wrap: anywhere;
}
.tool-status { margin: 0; min-height: 1.3em; font-size: 0.82rem; color: var(--muted); }
.tool-actions { display: flex; justify-content: flex-end; gap: 8px; }

.table-form { padding: 16px 18px 18px; display: flex; flex-direction: column; gap: 12px; }
.table-form label { display: flex; align-items: center; gap: 10px; font-size: 0.92rem; font-weight: 500; }
.table-form input[type="number"] {
  width: 80px; height: 38px; padding: 0 10px; border: 1px solid var(--border);
  border-radius: 10px; font-size: 1rem; font-family: var(--font); background: var(--card); color: var(--text);
}
.table-form .action-btn { align-self: flex-end; }

@media (max-width: 700px) {
  body { padding: 12px; }
  .container { padding: 20px 16px 24px; border-radius: 20px; }
  .page-title { font-size: 1.5rem; }
  .toolbar { gap: 4px; padding: 6px; }
  .toolbar-btn { min-width: 40px; height: 40px; }
  .toolbar-group { padding-right: 4px; height: 40px; }
  .editor { padding: 14px; min-height: 260px; }
  .actions-row { flex-direction: column; align-items: stretch; }
  .actions-group { justify-content: center; }
}
