:root { color-scheme: dark;
  --bg: #0b0c10; --panel: #13151b; --panel-2: #1a1d25; --line: #262a35; --line-2: #323745;
  --text: #e9ecf3; --muted: #8a91a3; --accent: #ff3d57; --accent-2: #ff8a3d;
  --grad: linear-gradient(135deg, var(--accent), var(--accent-2)); --radius: 12px;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text);
  font: 13px/1.4 Inter, system-ui, -apple-system, Segoe UI, sans-serif; }
button, input, select, textarea { font: inherit; color: inherit; }
.muted { color: var(--muted); }
[hidden] { display: none !important; }

/* Top bar */
.topbar { height: 60px; display: flex; align-items: center; justify-content: space-between; padding: 0 18px;
  border-bottom: 1px solid var(--line); background: rgba(19,21,27,.85); backdrop-filter: blur(8px); }
.brand { display: flex; align-items: center; gap: 10px; }
.logo { width: 34px; height: 34px; border-radius: 10px; background: var(--grad); display: grid; place-items: center;
  box-shadow: 0 6px 20px -6px rgba(255,61,87,.7); }
.logo svg { width: 20px; height: 20px; color: #fff; }
.brand-text { display: flex; flex-direction: column; line-height: 1.1; }
.brand-text strong { font-size: 16px; letter-spacing: -.01em; }
.brand-text span { font-size: 11px; color: var(--muted); }
.top-actions { display: flex; align-items: center; gap: 8px; }
.divider { width: 1px; height: 24px; background: var(--line); margin: 0 4px; }

.btn { border: 1px solid var(--line-2); background: var(--panel-2); border-radius: 10px; padding: 8px 14px; cursor: pointer;
  font-weight: 600; transition: .15s; display: inline-flex; align-items: center; gap: 6px; }
.btn:hover { border-color: #4a5163; background: #20242e; }
.btn small { font-weight: 500; opacity: .7; font-size: 11px; }
.btn.primary { background: var(--grad); border-color: transparent; color: #fff; box-shadow: 0 8px 24px -10px rgba(255,61,87,.9); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: var(--panel-2); }
.btn.icon { width: 36px; justify-content: center; padding: 6px; font-size: 18px; }
.btn:disabled { opacity: .35; cursor: default; }
.toggle { display: flex; align-items: center; gap: 6px; color: var(--muted); cursor: pointer; user-select: none; }
.toggle input { accent-color: var(--accent); }

/* Layout */
.app { display: grid; grid-template-columns: 290px 1fr 310px; height: calc(100vh - 60px); }
.panel { background: var(--panel); overflow-y: auto; }
.panel.left { border-right: 1px solid var(--line); }
.panel.right { border-left: 1px solid var(--line); }
.panel section { padding: 16px; border-bottom: 1px solid var(--line); }
.panel h3 { margin: 0 0 12px; font-size: 11px; text-transform: uppercase; letter-spacing: .09em; color: var(--muted); font-weight: 600; }
.panel::-webkit-scrollbar { width: 8px; } .panel::-webkit-scrollbar-thumb { background: var(--line-2); border-radius: 8px; }

.templates { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.tpl { padding: 0; border: 2px solid transparent; border-radius: 10px; background: var(--panel-2); cursor: pointer; overflow: hidden;
  text-align: left; transition: .15s; }
.tpl:hover { border-color: var(--line-2); transform: translateY(-1px); }
.tpl.active { border-color: var(--accent); }
.tpl canvas { display: block; width: 100%; aspect-ratio: 16/9; }
.tpl span { display: block; padding: 6px 8px; font-size: 11.5px; font-weight: 600; }

.add-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.tile { background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px; padding: 12px 8px; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 4px; font-weight: 600; font-size: 12px; transition: .15s; }
.tile span { font-size: 18px; font-weight: 800; }
.tile:hover { border-color: var(--accent); }
.shape-row { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; }
.chip { background: var(--panel-2); border: 1px solid var(--line-2); border-radius: 999px; padding: 5px 11px; cursor: pointer; font-size: 12px; }
.chip:hover { border-color: var(--accent); }
.link { background: none; border: none; color: var(--accent-2); padding: 10px 0 0; cursor: pointer; font-weight: 600; }
.link:hover { text-decoration: underline; }

.seg { display: flex; background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px; padding: 3px; margin-bottom: 10px; }
.seg button { flex: 1; border: none; background: none; padding: 6px; border-radius: 7px; cursor: pointer; color: var(--muted); font-weight: 600; }
.seg button.on { background: #2a2f3b; color: var(--text); }

.row { display: grid; grid-template-columns: 92px 1fr auto; align-items: center; gap: 8px; margin: 8px 0; min-height: 28px; }
.row > label { color: var(--muted); font-size: 12px; }
.row .val { font-size: 11px; color: var(--muted); min-width: 30px; text-align: right; font-variant-numeric: tabular-nums; }
.row.full { grid-template-columns: 1fr; }
input[type=range] { width: 100%; accent-color: var(--accent); }
input[type=color] { width: 100%; height: 28px; border: 1px solid var(--line-2); border-radius: 8px; background: var(--panel-2); padding: 2px; cursor: pointer; }
select, textarea, input[type=text] { width: 100%; background: var(--panel-2); border: 1px solid var(--line-2); border-radius: 8px; padding: 7px 9px; outline: none; }
/* iOS paints native selects/options with system colours unless told otherwise: keep them dark with light text */
select { -webkit-appearance: none; appearance: none; color: var(--text); background-color: var(--panel-2);
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 16px) 50%, calc(100% - 11px) 50%; background-size: 5px 5px; background-repeat: no-repeat; padding-right: 28px; }
option { color: var(--text); background-color: var(--panel-2); }
select:focus, textarea:focus, input[type=text]:focus { border-color: var(--accent); }
textarea { resize: vertical; min-height: 64px; font-weight: 600; }
.check { display: flex; align-items: center; gap: 6px; cursor: pointer; } .check input { accent-color: var(--accent); }
.subhead { margin: 16px 0 4px; font-size: 11px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .08em;
  display: flex; align-items: center; justify-content: space-between; }
.prop-actions { display: flex; gap: 6px; margin-top: 14px; }
.prop-actions .btn { flex: 1; justify-content: center; padding: 7px; font-size: 12px; }
.btn.danger:hover { border-color: var(--accent); color: #ff8e9c; }
.font-select { font-size: 15px; }

/* Stage */
.stage-wrap { display: flex; flex-direction: column; min-width: 0; padding: 24px; gap: 16px;
  background: radial-gradient(1200px 600px at 50% 30%, #161925 0%, var(--bg) 70%); }
.stage { position: relative; flex: 1; min-height: 0; display: grid; place-items: center; }
#canvas { max-width: 100%; max-height: 100%; aspect-ratio: 16 / 9; border-radius: 10px; touch-action: none;
  box-shadow: 0 30px 80px -20px rgba(0,0,0,.8), 0 0 0 1px var(--line-2); background: #000; }
.drop-hint { position: absolute; inset: 0; display: grid; place-items: center; font-size: 20px; font-weight: 700; pointer-events: none;
  background: rgba(255,61,87,.12); border: 2px dashed var(--accent); border-radius: 12px; opacity: 0; transition: .15s; }
.stage.dragging .drop-hint { opacity: 1; }
.stage-footer { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; font-size: 12px; }
.feed-preview { display: flex; flex-direction: column; gap: 6px; }
.feed-card { width: 250px; }
.feed-card canvas { width: 250px; height: 140.6px; border-radius: 8px; display: block; }
.feed-meta { display: flex; gap: 8px; margin-top: 8px; }
.feed-meta .avatar { width: 28px; height: 28px; border-radius: 50%; background: var(--grad); flex: none; }
.feed-meta b { display: block; font-size: 12.5px; line-height: 1.3; }
.feed-meta span { color: var(--muted); font-size: 11px; }

/* Layers */
.layers { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.layers li { display: flex; align-items: center; gap: 8px; padding: 7px 8px; border-radius: 8px; cursor: pointer; border: 1px solid transparent; }
.layers li:hover { background: var(--panel-2); }
.layers li.sel { background: #2a1820; border-color: rgba(255,61,87,.5); }
.layers .kind { width: 24px; height: 24px; border-radius: 6px; background: var(--panel-2); display: grid; place-items: center; font-size: 11px; font-weight: 800; flex: none; }
.layers .name { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.layers .mini-btn { border: none; background: none; color: var(--muted); cursor: pointer; padding: 2px 4px; border-radius: 4px; }
.layers .mini-btn:hover { color: var(--text); background: var(--line); }
.layers li.hiddenL .name { opacity: .4; }

.toast { position: fixed; bottom: 24px; left: 50%; transform: translate(-50%, 20px); background: #222633; border: 1px solid var(--line-2);
  padding: 10px 16px; border-radius: 10px; opacity: 0; transition: .2s; pointer-events: none; font-weight: 600; z-index: 60; max-width: min(560px, calc(100vw - 24px)); text-align: center; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 1100px) { .app { grid-template-columns: 250px 1fr 270px; } .feed-preview { display: none; } }
.plan-pill { font-size: 9.5px; font-weight: 700; padding: 2px 6px; border-radius: 999px; background: #2a2f3b; color: #c3c8d4; letter-spacing: .06em; }
a.btn { text-decoration: none; }
.ad-slot span { font-size: 10px; color: var(--muted); text-transform: uppercase; letter-spacing: .08em; }
.ad-box { margin-top: 6px; height: 250px; border: 1px dashed var(--line-2); border-radius: 10px; display: grid; place-items: center; color: var(--muted); }
.tpl span { display: flex; align-items: center; justify-content: space-between; gap: 4px; }
.pro-tag { font-style: normal; font-size: 9px; font-weight: 800; letter-spacing: .06em; padding: 2px 6px; border-radius: 999px; background: var(--grad); color: #fff; }
a.btn { color: var(--text); }

/* ===================== Mobile / touch ===================== */
/* Desktop (>900px) keeps the 3-column layout; these elements only exist on small screens. */
.tabbar, .touch-hint, .mobile-only, .sheet-scrim { display: none; }
.ctx-bar { display: none !important; }
.sheet-close { display: none; }
#canvas { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
.img-overlay { position: fixed; inset: 0; z-index: 50; background: rgba(5,6,9,.88); display: grid; place-items: center; padding: 16px; }
.img-overlay-card { width: min(100%, 720px); background: var(--panel); border: 1px solid var(--line-2); border-radius: 16px; padding: 14px; text-align: center; }
.img-overlay-card img { width: 100%; border-radius: 10px; display: block; -webkit-touch-callout: default; }
.img-overlay-card p { color: var(--muted); margin: 12px 0; font-size: 14px; }
.img-overlay-actions { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }
.img-overlay-actions .btn { min-height: 44px; padding: 10px 18px; }

/* Font previews in the Font select (classes, not inline styles: the CSP blocks style attributes) */
.ff-anton { font-family: "Anton", sans-serif; }
.ff-bebas-neue { font-family: "Bebas Neue", sans-serif; }
.ff-bangers { font-family: "Bangers", sans-serif; }
.ff-luckiest-guy { font-family: "Luckiest Guy", sans-serif; }
.ff-montserrat { font-family: "Montserrat", sans-serif; }
.ff-poppins { font-family: "Poppins", sans-serif; }
.ff-russo-one { font-family: "Russo One", sans-serif; }
.ff-permanent-marker { font-family: "Permanent Marker", sans-serif; }
.storage-warn { margin: 0 auto; max-width: 720px; padding: 8px 12px; border-radius: 10px; background: #3a2a10; border: 1px solid #a8741a; color: #ffd98a; font-size: 13px; text-align: center; }
.storage-warn[hidden] { display: none; }
.btn[disabled], .btn[aria-busy=true] { opacity: .6; cursor: progress; }
.layers .mini-btn.del:hover { color: #ff6b7d; }

@media (max-width: 900px), (max-height: 520px) and (orientation: landscape) and (pointer: coarse) {
  :root { --top: 52px; --tab: 60px; --safe-b: env(safe-area-inset-bottom, 0px); --kb: 0px; }
  html, body { height: 100%; overflow: hidden; overscroll-behavior: none; }
  body { font-size: 14px; position: fixed; inset: 0; }
  button, a, label, summary { touch-action: manipulation; -webkit-tap-highlight-color: transparent; }

  /* Top bar: brand + undo/redo + home. Export and toggles move into the Export sheet. */
  .topbar { height: var(--top); padding: 0 10px 0 12px; padding-top: env(safe-area-inset-top, 0px); }
  .brand-text span, .divider { display: none; }
  .logo { width: 30px; height: 30px; border-radius: 9px; }
  .top-actions { gap: 4px; }
  .top-actions .btn { min-height: 44px; }
  .btn.icon { width: 44px; height: 44px; font-size: 20px; }

  /* Canvas on top, full width */
  .app { display: block; height: calc(100% - var(--top)); }
  .panel { display: contents; }
  .stage-wrap { padding: 10px 10px calc(var(--tab) + var(--safe-b) + 10px); gap: 10px; height: 100%; overflow: hidden; touch-action: none; }
  .stage { flex: none; display: block; }
  #canvas { display: block; width: 100%; height: auto; max-height: none; border-radius: 8px; }
  .stage-footer { display: block; font-size: 12.5px; text-align: center; }
  .desk-hint { display: none; }
  .touch-hint { display: block; padding: 4px 8px; }
  body.has-sel .touch-hint { display: none; }
  .ctx-bar { display: flex !important; gap: 6px; justify-content: center; flex-wrap: wrap; }
  .ctx-bar[hidden] { display: none !important; }
  .ctx-bar .btn { min-height: 44px; min-width: 44px; padding: 8px 12px; justify-content: center; font-size: 14px; }

  /* Bottom tab bar */
  .tabbar { display: grid; grid-template-columns: repeat(6, 1fr); position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
    height: calc(var(--tab) + var(--safe-b)); padding-bottom: var(--safe-b); background: rgba(19,21,27,.97); border-top: 1px solid var(--line); backdrop-filter: blur(10px); }
  .tabbar button { border: 0; background: none; color: var(--muted); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
    font-size: 10.5px; font-weight: 600; letter-spacing: -.01em; min-height: 44px; padding: 0 2px; cursor: pointer; white-space: nowrap; }
  .tabbar button i { font-style: normal; font-size: 19px; line-height: 1; font-weight: 800; }
  .tabbar button.on { color: var(--text); }
  .tabbar button.on i { color: var(--accent); }

  /* Panel sections become bottom sheets */
  .panel section[data-sheet] { position: fixed; left: 0; right: 0; z-index: 25; bottom: calc(var(--tab) + var(--safe-b));
    max-height: min(calc(100dvh - var(--top) - var(--tab) - var(--safe-b) - 8px), max(200px, calc(100dvh - var(--top) - 56.25vw - var(--tab) - var(--safe-b) - 16px)));
    overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; touch-action: pan-y;
    background: var(--panel); border: 1px solid var(--line-2); border-bottom: 0; border-radius: 18px 18px 0 0; padding: 0 16px 18px;
    box-shadow: 0 -20px 50px -20px rgba(0,0,0,.9); transform: translateY(calc(100% + var(--tab) + 40px)); visibility: hidden;
    transition: transform .22s ease, visibility 0s linear .22s; }
  .panel section[data-sheet].open { transform: none; visibility: visible; transition: transform .22s ease; }
  .panel section.ad-slot { display: none; }
  .panel section[data-sheet] > h3 { position: sticky; top: 0; z-index: 2; margin: 0 -16px 12px; padding: 18px 16px 12px; background: var(--panel);
    display: flex; align-items: center; justify-content: space-between; font-size: 12px; }
  .panel section[data-sheet] > h3::before { content: ''; position: absolute; top: 7px; left: 50%; width: 38px; height: 4px; margin-left: -19px; border-radius: 4px; background: var(--line-2); }
  .sheet-close { display: inline-grid; place-items: center; border: 0; background: var(--panel-2); color: var(--text); width: 36px; height: 36px; border-radius: 50%; font-size: 16px; cursor: pointer; margin: -8px -4px -8px 0; }
  .sheet-close::after { content: ''; position: absolute; inset: -6px; }
  .sheet-close { position: relative; }
  .mobile-only { display: block; }
  .quick { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 6px; }
  .quick .btn { min-height: 44px; font-size: 14px; }
  body.has-sel .quick { display: none; }

  /* Keyboard open: sheet sits right above the keyboard, tab bar hides */
  body.typing .tabbar { display: none; }
  body.typing .panel section[data-sheet] { bottom: var(--kb); max-height: min(calc(100dvh - var(--kb) - 4px), max(160px, calc(100dvh - var(--kb) - var(--top) - 56.25vw - 16px))); }

  /* Touch-sized controls inside sheets */
  .templates { grid-template-columns: repeat(3, 1fr); gap: 8px; }
  .tpl span { padding: 6px 7px; font-size: 12px; min-height: 30px; }
  .add-grid { grid-template-columns: repeat(4, 1fr); }
  .tile { min-height: 64px; padding: 10px 4px; font-size: 12px; }
  .chip { min-height: 40px; padding: 8px 14px; font-size: 13px; }
  .link { min-height: 44px; padding: 10px 0; font-size: 14px; }
  .seg button { min-height: 40px; font-size: 14px; }
  .row { grid-template-columns: 96px 1fr auto; min-height: 44px; margin: 4px 0; }
  .row > label { font-size: 13.5px; }
  input[type=color] { height: 38px; }
  input[type=range] { height: 32px; }
  input[type=checkbox] { width: 22px; height: 22px; }
  /* 16px stops iOS Safari zooming the page when a field gets focus */
  select, textarea, input[type=text] { font-size: 16px; min-height: 44px; padding: 9px 11px; }
  select { padding-right: 30px; }
  textarea { min-height: 84px; }
  .prop-actions .btn { min-height: 44px; font-size: 14px; }
  .layers li { min-height: 48px; padding: 6px 8px; }
  .layers .mini-btn { min-width: 40px; min-height: 40px; font-size: 14px; }
  .export-btns { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
  .export-btns .btn { min-height: 52px; justify-content: center; font-size: 15px; flex-direction: column; gap: 0; }
  .export-toggles { display: flex; flex-direction: column; gap: 2px; margin: 12px 0 4px; }
  .export-toggles .toggle { min-height: 44px; font-size: 15px; color: var(--text); gap: 10px; }
  .feed-details summary { min-height: 44px; display: flex; align-items: center; font-weight: 600; cursor: pointer; color: var(--muted); }
  .feed-details .feed-preview { display: flex; }
  .feed-details .feed-preview > .muted { display: none; }
  .feed-details .feed-card, .feed-details .feed-card canvas { width: 100%; height: auto; aspect-ratio: 16/9; }
  .feed-details .feed-card { aspect-ratio: auto; }
  p.small { font-size: 12.5px; margin: 10px 0 0; }
  .toast { bottom: calc(var(--tab) + var(--safe-b) + 14px); max-width: calc(100vw - 24px); text-align: center; z-index: 60; }
}
/* Landscape phones: tools become a left rail, sheets a right-side panel, and the canvas is sized by the
   smaller of the free width and height so the whole 16:9 canvas is always on screen. */
@media (max-width: 900px) and (orientation: landscape), (max-height: 520px) and (orientation: landscape) and (pointer: coarse) {
  :root { --top: 44px; --rail: calc(64px + env(safe-area-inset-left, 0px)); --side: 0px; --col: 112px; }
  body[data-sheet] { --side: min(360px, 46vw); --col: 0px; }
  body[data-sheet] .quick, body[data-sheet] .ctx-bar { display: none !important; }
  .topbar { height: var(--top); }
  .btn.icon { width: 40px; height: 40px; }
  .top-actions .btn { min-height: 40px; }
  .tabbar { top: var(--top); bottom: 0; right: auto; width: var(--rail); height: auto; padding: 0 0 var(--safe-b) env(safe-area-inset-left, 0px);
    grid-template-columns: 1fr; grid-template-rows: repeat(6, minmax(44px, 1fr)); border-top: 0; border-right: 1px solid var(--line); }
  .stage-wrap { flex-direction: row; align-items: center; justify-content: center; gap: 10px;
    padding: 8px calc(10px + var(--side) + env(safe-area-inset-right, 0px)) calc(8px + var(--safe-b)) calc(var(--rail) + 10px); }
  .stage { flex: none; }
  #canvas { width: min(calc(100vw - var(--rail) - var(--side) - var(--col) - 40px - env(safe-area-inset-right, 0px)), calc((100dvh - var(--top) - 16px - var(--safe-b)) * 16 / 9)); height: auto; }
  .stage-footer { display: none; }
  .quick, .ctx-bar { flex: none; width: var(--col); flex-direction: column; flex-wrap: nowrap; align-items: stretch; margin: 0; }
  .quick .btn, .ctx-bar .btn { white-space: normal; line-height: 1.15; font-size: 13px; padding: 6px 8px; }
  .storage-warn { position: fixed; top: calc(var(--top) + 6px); left: 50%; transform: translateX(-50%); z-index: 26; max-width: 70vw; }
  .panel section[data-sheet] { top: var(--top); bottom: 0; left: auto; right: 0; width: var(--side); max-height: none;
    border: 0; border-left: 1px solid var(--line-2); border-radius: 18px 0 0 18px; padding: 0 calc(14px + env(safe-area-inset-right, 0px)) calc(14px + var(--safe-b)) 14px;
    box-shadow: -20px 0 50px -20px rgba(0,0,0,.9); transform: translateX(calc(100% + 40px)); }
  .panel section[data-sheet] > h3 { margin: 0 -14px 10px; padding: 12px 14px 10px; }
  .panel section[data-sheet] > h3::before { display: none; }
  /* Typing: the panel spans the whole height above the keyboard so its header can never leave the screen */
  body.typing .panel section[data-sheet] { top: 0; bottom: var(--kb); max-height: none; }
  .toast { bottom: calc(12px + var(--safe-b)); }
  .templates { grid-template-columns: 1fr 1fr; }
  .add-grid { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 420px) {
  .brand-text strong { font-size: 15px; }
  .templates { grid-template-columns: 1fr 1fr; }
  .add-grid { grid-template-columns: repeat(4, 1fr); gap: 6px; }
  .tile span { font-size: 16px; }
  .tile { font-size: 11px; }
}
@media (max-width: 370px) { .brand-text { display: none; } }
