:root {
  --bg: #0f172a;
  --surface: #1e293b;
  --border: #334155;
  --text: #f1f5f9;
  --muted: #94a3b8;
  --accent: #38bdf8;
  --accent-dim: #0ea5e9;
  --danger: #f87171;
  --radius: 12px;
  --safe-bottom: env(safe-area-inset-bottom, 0);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

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

html {
  -webkit-tap-highlight-color: transparent;
}

body {
  margin: 0;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  padding-bottom: calc(16px + var(--safe-bottom));
}

.app-header {
  padding: 16px 16px 8px;
  text-align: center;
}

.app-header h1 {
  margin: 0;
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.tagline {
  margin: 6px 0 0;
  font-size: 0.85rem;
  color: var(--muted);
}

.home-nav {
  margin-bottom: 8px;
}

.home-nav__link {
  color: var(--accent);
  text-decoration: none;
  font-size: 0.9rem;
}

.home-nav__link:hover {
  text-decoration: underline;
}

.app-main {
  max-width: 560px;
  margin: 0 auto;
  padding: 0 12px 24px;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 14px;
}

.segmented {
  display: flex;
  gap: 0;
  margin-bottom: 12px;
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid var(--border);
}

.seg {
  flex: 1;
  padding: 10px 12px;
  border: none;
  background: var(--bg);
  color: var(--muted);
  font-size: 0.95rem;
  cursor: pointer;
}

.seg.active {
  background: var(--accent);
  color: #0f172a;
  font-weight: 600;
}

.panel-body.hidden {
  display: none;
}

.hint {
  font-size: 0.8rem;
  color: var(--muted);
  margin: 0 0 10px;
  line-height: 1.4;
}

.hint code {
  font-size: 0.75rem;
  background: var(--bg);
  padding: 2px 6px;
  border-radius: 4px;
}

.template-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  max-height: 220px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.template-thumb {
  position: relative;
  aspect-ratio: 1;
  border-radius: 8px;
  overflow: hidden;
  border: 2px solid transparent;
  cursor: pointer;
  background: var(--bg);
}

.template-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.template-thumb.selected {
  border-color: var(--accent);
}

.template-thumb:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.file-label {
  display: block;
  text-align: center;
  padding: 14px;
  background: var(--bg);
  border-radius: 10px;
  border: 1px dashed var(--border);
  cursor: pointer;
  color: var(--accent);
  font-weight: 500;
}

.status {
  font-size: 0.8rem;
  color: var(--muted);
  margin: 8px 0 0;
  min-height: 1.2em;
}

.status.error {
  color: var(--danger);
}

.canvas-wrap {
  position: relative;
  width: 100%;
  border-radius: 10px;
  overflow: hidden;
  background: #000;
  touch-action: none;
}

#memeCanvas {
  display: block;
  width: 100%;
  height: auto;
  vertical-align: middle;
  user-select: none;
  -webkit-user-select: none;
}

.meme-text-overlay {
  position: absolute;
  z-index: 2;
  margin: 0;
  padding: 4px 6px;
  box-sizing: border-box;
  overflow: hidden;
  resize: none;
  border: none;
  border-radius: 4px;
  background: rgba(0, 0, 0, 0.25);
  color: #fff;
  font-family: Impact, "Arial Black", sans-serif;
  font-weight: 700;
  text-align: center;
  line-height: 1.15;
  caret-color: #fff;
  -webkit-text-stroke: 1.5px #000;
  paint-order: stroke fill;
  text-shadow:
    1px 1px 0 #000,
    -1px -1px 0 #000,
    1px -1px 0 #000,
    -1px 1px 0 #000;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.12s ease;
}

.meme-text-overlay:not(:disabled).is-editing {
  opacity: 1;
  pointer-events: auto;
}

.meme-text-overlay:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.meme-text-overlay:disabled {
  cursor: not-allowed;
}

.meme-drag-handle {
  position: absolute;
  z-index: 4;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  margin: 0;
  padding: 0;
  border: 2px solid rgba(255, 255, 255, 0.9);
  border-radius: 999px;
  background: rgba(15, 23, 42, 0.92);
  color: var(--accent);
  cursor: grab;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.45);
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
}

.meme-drag-handle:active {
  cursor: grabbing;
}

.meme-drag-handle:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.meme-drag-handle[hidden] {
  display: none !important;
}

.meme-drag-handle__icon {
  display: block;
  width: 14px;
  height: 2px;
  border-radius: 1px;
  background: currentColor;
  box-shadow: 0 5px 0 currentColor, 0 10px 0 currentColor;
  user-select: none;
  pointer-events: none;
}

.editor-hint {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 3;
  margin: 0;
  padding: 6px 8px;
  font-size: 0.7rem;
  color: rgba(255, 255, 255, 0.65);
  background: linear-gradient(transparent, rgba(0, 0, 0, 0.65));
  pointer-events: none;
}

.btn {
  flex: 1;
  min-width: 120px;
  padding: 12px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 0.95rem;
  cursor: pointer;
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn.primary {
  background: var(--accent);
  border-color: var(--accent-dim);
  color: #0f172a;
  font-weight: 600;
}

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

.save-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

@media (min-width: 480px) {
  .save-actions {
    flex-direction: row;
  }
}
