/* Invoice templates: list, drag and drop block editor, HTML code editor, image picker. Solid colours only. */

/* ---------- list ---------- */
.tpl-page .tpl-section { margin-top: 28px; }
.tpl-page .tpl-section > h2 { font-size: 17px; margin: 0 0 4px; }
.tpl-page .tpl-section > p { margin: 0 0 12px; }
.tpl-defaults { margin-top: 4px; padding: 16px 18px; }
.tpl-defaults .help { margin: 4px 0 0; }
.tpl-defaults .grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px 16px; }
.tpl-list { display: grid; gap: 8px; margin-top: 10px; }
.tpl-row { display: flex; align-items: center; gap: 12px; padding: 10px 12px; flex-wrap: wrap; }
.tpl-row-main { display: flex; align-items: center; gap: 12px; flex: 1 1 260px; min-width: 0; color: var(--text); }
.tpl-row-main:hover { text-decoration: none; }
.tpl-row-main:hover b { color: var(--accent-text); }
.tpl-row-main > span:last-child { display: flex; flex-direction: column; min-width: 0; }
.tpl-row-main b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tpl-row-ic { width: 36px; height: 36px; border-radius: 8px; background: var(--surface-2); display: inline-flex; align-items: center; justify-content: center; flex: none; }
.tpl-row-tags { display: flex; gap: 6px; flex-wrap: wrap; }
.tpl-row-actions { display: flex; gap: 4px; margin-left: auto; }
.tpl-designs { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 12px; }
.tpl-design, .tpl-example { display: flex; flex-direction: column; gap: 4px; text-align: left; padding: 10px; border: 1px solid var(--border); border-radius: var(--radius-lg); background: var(--surface); color: var(--text); cursor: pointer; font: inherit; }
.tpl-design:hover:not(:disabled), .tpl-example:hover:not(:disabled) { border-color: var(--accent); box-shadow: var(--shadow); }
.tpl-design:disabled, .tpl-example:disabled { opacity: .6; cursor: not-allowed; }
.tpl-design img { width: 100%; aspect-ratio: 210 / 297; object-fit: cover; object-position: top; border-radius: 6px; border: 1px solid var(--border); background: #fff; }
.tpl-design small, .tpl-example small { line-height: 1.35; }
.tpl-examples { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; }
.tpl-example .i { width: 22px; height: 22px; color: var(--accent-text); }

/* ---------- editor frame ---------- */
body.tpl-editing .site-footer { display: none; }
.tpl-editor { display: flex; flex-direction: column; height: calc(100dvh - var(--header-h)); min-height: 480px; background: var(--bg); }
.tpl-bar { display: flex; align-items: center; gap: 10px; padding: 8px 14px; background: var(--surface); border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.tpl-bar-name { display: flex; gap: 8px; align-items: center; flex: 1 1 280px; min-width: 0; }
.tpl-name { flex: 1 1 auto; min-width: 0; font-weight: 600; font-size: 15px; height: 36px; }
.tpl-kind { width: auto; height: 36px; }
.tpl-bar-tools { display: flex; gap: 4px; align-items: center; }
.tpl-bar-tools select { height: 36px; width: auto; }
.tpl-bar-end { display: flex; gap: 8px; align-items: center; margin-left: auto; }
.tpl-status { white-space: nowrap; }
.tpl-mtabs { display: none; }
.tpl-body { flex: 1; min-height: 0; display: grid; grid-template-columns: 200px minmax(0, 1fr) 340px; }

/* palette */
.tpl-palette { overflow-y: auto; padding: 12px; border-right: 1px solid var(--border); background: var(--surface); }
.tpl-pal-title { margin: 0; font-size: 14px; }
.tpl-palette > p { margin: 2px 0 10px; }
.tpl-pal-group h4 { margin: 12px 0 6px; font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.tpl-pal-items { display: grid; gap: 4px; }
.tpl-pal-item { display: flex; align-items: center; gap: 8px; padding: 7px 9px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); color: var(--text); font: inherit; font-size: 13px; cursor: grab; touch-action: none; user-select: none; text-align: left; }
.tpl-pal-item:hover { border-color: var(--accent); background: var(--surface-2); }
.tpl-pal-item:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.tpl-pal-item .i { color: var(--accent-text); flex: none; }

/* stage */
.tpl-stage { overflow: auto; padding: 28px 24px 80px; background: var(--canvas-bg); position: relative; }
.tpl-zoom { margin: 0 auto; position: relative; }
.tpl-canvas-host { transform-origin: 0 0; box-shadow: 0 2px 12px rgba(0, 0, 0, .18); background: #fff; }
.tpl-guide { position: fixed; z-index: 400; background: #f97316; pointer-events: none; border-radius: 2px; box-shadow: 0 0 0 1px #fff; }
.tpl-guide-label { position: absolute; left: 6px; top: -22px; background: #f97316; color: #fff; font: 600 11px/1 system-ui, sans-serif; padding: 4px 6px; border-radius: 4px; white-space: nowrap; display: none; }
.tpl-guide.labelled .tpl-guide-label { display: block; }
.tpl-ghost { position: fixed; left: 0; top: 0; z-index: 401; pointer-events: none; background: var(--accent); color: var(--on-accent); padding: 6px 10px; border-radius: 8px; font-size: 13px; font-weight: 600; box-shadow: var(--shadow-lg); }

/* side panel */
.tpl-side { overflow-y: auto; border-left: 1px solid var(--border); background: var(--surface); display: flex; flex-direction: column; min-height: 0; }
.tpl-sheet-close { display: none; }
.tpl-tabs { display: flex; gap: 2px; padding: 8px 10px 0; border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--surface); z-index: 2; }
.tpl-tab { flex: 1; border: 0; background: none; padding: 8px 6px 9px; font: inherit; font-size: 13px; font-weight: 600; color: var(--muted); border-bottom: 2px solid transparent; cursor: pointer; }
.tpl-tab.on { color: var(--accent-text); border-bottom-color: var(--accent); }
.tpl-panel { padding: 12px 14px 24px; }
.tpl-hint { text-align: center; color: var(--text-2); padding: 24px 8px; }
.tpl-hint .i { width: 28px; height: 28px; color: var(--muted); }
.tpl-props-head { display: flex; align-items: center; gap: 8px; margin-bottom: 4px; }
.tpl-props-head b { flex: 1; }
.tpl-group { border-top: 1px solid var(--border); padding: 10px 0 4px; }
.tpl-props > .tpl-group:first-of-type { border-top: 0; }
.tpl-group h4 { margin: 0 0 8px; font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.tpl-group h4:not(:first-child) { margin-top: 14px; }
.tpl-field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; font-size: 13px; }
.tpl-field > span { color: var(--text-2); font-weight: 500; }
.tpl-field input[type=text], .tpl-field textarea, .tpl-field select, .tpl-num input { width: 100%; }
.tpl-two { display: grid; grid-template-columns: 1fr 1fr; gap: 0 10px; }
.tpl-num { display: flex; align-items: center; gap: 6px; }
.tpl-num input { min-width: 0; }
.tpl-pad { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.tpl-pad label { display: flex; flex-direction: column; gap: 2px; }
.tpl-pad input { width: 100%; min-width: 0; padding-left: 6px; padding-right: 4px; }
.tpl-snaps { display: flex; flex-wrap: wrap; gap: 4px; margin: -4px 0 10px; }
.tpl-snaps .btn { height: 26px; padding: 0 8px; font-size: 12px; }
.tpl-checks { display: flex; gap: 14px; flex-wrap: wrap; margin-bottom: 10px; }
.tpl-group .check { display: flex; gap: 8px; align-items: center; font-size: 13px; margin-bottom: 8px; }
.tpl-seg { display: inline-flex; border: 1px solid var(--border-strong); border-radius: 8px; overflow: hidden; }
.tpl-seg button { border: 0; background: var(--surface); color: var(--text-2); padding: 6px 12px; font: inherit; font-size: 13px; cursor: pointer; display: inline-flex; align-items: center; }
.tpl-seg button + button { border-left: 1px solid var(--border); }
.tpl-seg button.on { background: var(--accent); color: var(--on-accent); }
.tpl-colors { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.tpl-sw { width: 26px; height: 26px; border-radius: 50%; border: 2px solid var(--border-strong); cursor: pointer; padding: 0; display: inline-flex; align-items: center; justify-content: center; background: var(--surface); color: var(--muted); }
.tpl-sw.on, .tpl-colors input.on { box-shadow: 0 0 0 2px var(--surface), 0 0 0 4px var(--accent); }
.tpl-sw.tok-accent { background: var(--brand, #047857); }
.tpl-sw.tok-accent-tint { background: color-mix(in srgb, var(--brand, #047857) 13%, #fff); }
.tpl-sw.tok-accent-soft { background: color-mix(in srgb, var(--brand, #047857) 6%, #fff); }
.tpl-sw.tok-ink { background: #1c2129; }
.tpl-sw.tok-muted { background: #6b7280; }
.tpl-sw.tok-white { background: #fff; }
.tpl-colors input[type=color] { width: 30px; height: 28px; padding: 0 2px; border-radius: 6px; cursor: pointer; }
.tpl-imgrow { display: flex; align-items: center; gap: 10px; }
.tpl-imgrow img { width: 54px; height: 40px; object-fit: contain; border: 1px solid var(--border); border-radius: 6px; background: #fff; }
.tpl-cols { display: grid; gap: 6px; margin-bottom: 8px; }
.tpl-col { display: grid; grid-template-columns: auto 1fr auto auto; gap: 4px 6px; align-items: center; }
.tpl-col input[type=text] { min-width: 0; height: 32px; }
.tpl-col .btn.icon { width: 30px; height: 30px; }
.tpl-col-note { grid-column: 2 / -1; margin-top: -2px; font-size: 11px; }
.tpl-add-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }

/* preview */
.tpl-preview { display: flex; flex-direction: column; gap: 8px; margin-top: 10px; }
.tpl-prev-head { display: flex; align-items: center; gap: 8px; }
.tpl-prev-head b { margin-right: auto; }
.tpl-prev-head .btn.small, .btn.small { height: 28px; padding: 0 10px; font-size: 12px; }
.tpl-prev-paper { background: var(--canvas-bg); border-radius: 8px; padding: 10px; min-height: 200px; display: flex; justify-content: center; }
.tpl-prev-img { width: 100%; max-width: 640px; height: auto; background: #fff; box-shadow: 0 1px 6px rgba(0, 0, 0, .18); align-self: flex-start; min-height: 120px; }
.tpl-preview.busy .tpl-prev-img { opacity: .55; transition: opacity .2s; }
.tpl-pager { display: flex; align-items: center; justify-content: center; gap: 8px; }
.tpl-pager[hidden] { display: none; }

/* phone bar (hidden on desktop) */
.tpl-phonebar { display: none; }

/* ---------- HTML code editor ---------- */
.tpl-code-body { flex: 1; min-height: 0; display: grid; grid-template-columns: minmax(0, 1.1fr) minmax(0, 1fr); }
.tpl-code-pane { display: flex; flex-direction: column; min-height: 0; border-right: 1px solid var(--border); background: var(--surface); }
.tpl-code { flex: 1; min-height: 0; position: relative; }
.tpl-code[hidden] { display: none; }
.tpl-code .CodeMirror { height: 100%; font: 13px/1.5 ui-monospace, "SF Mono", Menlo, Consolas, monospace; background: var(--surface); color: var(--text); }
.tpl-code .CodeMirror-gutters { background: var(--surface-2); border-right: 1px solid var(--border); }
.tpl-code .CodeMirror-linenumber { color: var(--muted); }
.tpl-code .CodeMirror-cursor { border-left-color: var(--text); }
.tpl-code .CodeMirror-selected { background: var(--accent-soft) !important; }
.tpl-code .tpl-err-line { background: var(--danger-soft); }
.tpl-code-fallback { width: 100%; height: 100%; border: 0; border-radius: 0; resize: none; padding: 12px; font: 13px/1.5 ui-monospace, "SF Mono", Menlo, Consolas, monospace; background: var(--surface); color: var(--text); }
.tpl-code.no-cm { display: flex; flex-direction: column; }
.tpl-cm-note { margin: 0; padding: 6px 12px; border-bottom: 1px solid var(--border); }
.tpl-error { display: flex; gap: 8px; align-items: center; padding: 8px 12px; background: var(--danger-soft); color: var(--danger); border-top: 1px solid var(--border); font-size: 13px; }
.tpl-error[hidden] { display: none; }
.tpl-error span { flex: 1; }
.tpl-code-preview { overflow-y: auto; padding: 12px 16px 30px; }
.tpl-code-preview .tpl-preview { margin-top: 8px; }
.tpl-ref { flex: 1; min-height: 0; overflow-y: auto; padding: 10px 14px 24px; }
.tpl-ref[hidden] { display: none; }
.tpl-ref input[type=search] { width: 100%; margin-bottom: 8px; }
.tpl-ref-group h4 { margin: 14px 0 6px; font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.tpl-ref-var { display: grid; grid-template-columns: minmax(0, auto) 1fr; gap: 2px 10px; align-items: baseline; padding: 4px 0; border-bottom: 1px solid var(--border); }
.tpl-ref-var[hidden] { display: none; }
.tpl-ref-ins { border: 0; background: none; padding: 0; cursor: pointer; text-align: left; color: var(--accent-text); font: inherit; }
.tpl-ref-ins code { font-size: 12px; background: var(--surface-2); padding: 1px 5px; border-radius: 4px; word-break: break-all; }
.tpl-ref-ins:hover code { background: var(--accent-soft); }
.tpl-ref-fields { grid-column: 1 / -1; display: flex; flex-wrap: wrap; gap: 4px; padding: 2px 0 2px 12px; }
.tpl-ref-notes { padding-left: 18px; color: var(--text-2); }

/* ---------- image picker ---------- */
.tpl-img-bar { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; margin-bottom: 12px; }
.tpl-img-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 10px; max-height: 50vh; overflow-y: auto; }
.tpl-img-cell { position: relative; }
.tpl-img { width: 100%; aspect-ratio: 1; border: 1px solid var(--border); border-radius: 8px; background: #fff; padding: 6px; cursor: pointer; display: flex; align-items: center; justify-content: center; }
.tpl-img:hover { border-color: var(--accent); }
.tpl-img img { max-width: 100%; max-height: 100%; object-fit: contain; }
.tpl-img-del { position: absolute; top: 4px; right: 4px; background: var(--surface); width: 28px; height: 28px; }
.tpl-img-empty { grid-column: 1 / -1; text-align: center; color: var(--muted); padding: 24px; }

/* invoice editor: line image */
.item-thumb { width: 40px; height: 40px; border-radius: 6px; border: 1px solid var(--border); object-fit: cover; background: #fff; cursor: pointer; padding: 0; flex: none; }
.item-main.has-thumb { display: grid; grid-template-columns: 40px 1fr; gap: 0 8px; align-items: start; }
.item-main.has-thumb > .item-thumb-btn { grid-row: span 2; }
.item-thumb-btn { border: 0; padding: 0; background: none; cursor: pointer; }
.design-picks.custom .design-pick .tpl-pick-ic { display: flex; align-items: center; justify-content: center; aspect-ratio: 210 / 297; width: 100%; background: var(--surface-2); border-radius: 4px; color: var(--accent-text); }

/* ---------- tablet ---------- */
@media (max-width: 1180px) {
  .tpl-body { grid-template-columns: 170px minmax(0, 1fr) 300px; }
  .tpl-stage { padding: 20px 14px 80px; }
}

/* ---------- phone ---------- */
@media (max-width: 760px) {
  .tpl-defaults .grid2 { grid-template-columns: 1fr; }
  .tpl-row-actions { margin-left: 0; width: 100%; justify-content: flex-end; }
  .tpl-designs { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .tpl-editor { height: calc(100dvh - var(--header-h)); }
  .tpl-bar { padding: 6px 10px; gap: 6px; }
  .tpl-bar-name { order: 1; flex: 1 1 calc(100% - 50px); }
  .tpl-kind { max-width: 118px; font-size: 13px; }
  .tpl-bar-tools { order: 2; }
  .tpl-bar-end { order: 3; margin-left: auto; }
  .tpl-bar .hide-sm { display: none; }
  .tpl-mtabs { display: flex; background: var(--surface); border-bottom: 1px solid var(--border); }
  .tpl-mtabs button { flex: 1; border: 0; background: none; padding: 9px; font: inherit; font-weight: 600; color: var(--muted); border-bottom: 2px solid transparent; }
  .tpl-mtabs button.on { color: var(--accent-text); border-bottom-color: var(--accent); }
  .tpl-body { display: block; position: relative; overflow: hidden; }
  .tpl-palette { display: none; }
  .tpl-stage { height: 100%; padding: 12px 8px 90px; }
  .tpl-blocks .tpl-side { position: fixed; left: 0; right: 0; bottom: 0; z-index: 300; max-height: 64dvh; border-left: 0; border-top: 1px solid var(--border-strong); border-radius: 16px 16px 0 0;
    box-shadow: 0 -8px 30px rgba(0, 0, 0, .22); transform: translateY(105%); transition: transform .2s ease; padding-bottom: var(--safe-b); }
  .tpl-blocks .tpl-side.open { transform: none; }
  .tpl-blocks .tpl-sheet-close { display: inline-flex; position: absolute; right: 8px; top: 6px; z-index: 3; }
  .tpl-blocks .tpl-tabs { padding-right: 48px; }
  .tpl-blocks.show-preview .tpl-stage { display: none; }
  .tpl-blocks.show-preview .tpl-side { position: static; transform: none; max-height: none; height: 100%; border-radius: 0; box-shadow: none; border-top: 0; }
  .tpl-blocks.show-preview .tpl-sheet-close { display: none; }
  .tpl-blocks.show-preview .tpl-phonebar { display: none; }
  body.tpl-editing .toasts { bottom: calc(70px + var(--safe-b)); }
  .tpl-phonebar { display: flex; gap: 6px; position: fixed; left: 0; right: 0; bottom: 0; z-index: 250; padding: 8px 10px calc(8px + var(--safe-b)); background: var(--surface); border-top: 1px solid var(--border); overflow-x: auto; }
  .tpl-phonebar .btn { flex: none; height: 40px; }
  .tpl-phonebar .btn.icon { width: 40px; }
  .tpl-two { grid-template-columns: 1fr 1fr; }
  .tpl-code-body { display: block; position: relative; overflow: hidden; }
  .tpl-code-pane { height: 100%; border-right: 0; }
  .tpl-code-pane > .tpl-tabs { display: none; }
  .tpl-code-preview { display: none; height: 100%; }
  .tpl-html[data-view="preview"] .tpl-code-pane { display: none; }
  .tpl-html[data-view="preview"] .tpl-code-preview { display: block; }
  .tpl-img-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .tpl-col { grid-template-columns: auto 1fr auto auto; }
}
