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

/*
 * Layers, as in Telegram's own settings screens:
 *   page (--page)  →  cards and lists (--surface)  →  controls inside them (--fill)
 * Secondary buttons are tinted with the button colour (--tint) so they always
 * read as buttons, whatever surface they sit on.
 */
:root {
  --page:      var(--tg-theme-secondary-bg-color, #efeff4);
  --surface:   var(--tg-theme-section-bg-color, var(--tg-theme-bg-color, #ffffff));
  --fill:      var(--tg-theme-secondary-bg-color, #efeff4);
  --text:      var(--tg-theme-text-color, #000000);
  --hint:      var(--tg-theme-hint-color, #8e8e93);
  --section:   var(--tg-theme-section-header-text-color, var(--hint));
  --link:      var(--tg-theme-link-color, #2481cc);
  --btn:       var(--tg-theme-button-color, #2481cc);
  --btn-text:  var(--tg-theme-button-text-color, #ffffff);
  --danger:    var(--tg-theme-destructive-text-color, #e53935);
  --divider:   var(--tg-theme-section-separator-color, rgba(128, 128, 128, .2));
  --tint:      rgba(36, 129, 204, .12);
  --tint:      color-mix(in srgb, var(--btn) 13%, transparent);
  --track:     rgba(128, 128, 128, .14);
  --track:     color-mix(in srgb, var(--text) 8%, transparent);
  --radius: 14px;
  --radius-sm: 10px;
}

/* Previewing outside Telegram: follow the system theme. Inside Telegram the
   --tg-theme-* values set by telegram-web-app.js win. */
@media (prefers-color-scheme: dark) {
  :root {
    --page: var(--tg-theme-secondary-bg-color, #000000);
    --surface: var(--tg-theme-section-bg-color, var(--tg-theme-bg-color, #1c1c1e));
    --fill: var(--tg-theme-secondary-bg-color, #2c2c2e);
    --text: var(--tg-theme-text-color, #ffffff);
    --hint: var(--tg-theme-hint-color, #98989f);
  }
}

html, body { background: var(--page); color: var(--text); }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  line-height: 1.35;
  min-height: 100vh;
  padding: 16px 16px 96px;
  -webkit-user-select: none; user-select: none;
  -webkit-tap-highlight-color: transparent;
}
input, textarea, select { -webkit-user-select: text; user-select: text; }

h1 { font-size: 22px; font-weight: 700; letter-spacing: -.2px; margin: 2px 0 4px; }
h2 {
  font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em;
  color: var(--section); margin: 22px 4px 8px;
}
h2:first-child { margin-top: 4px; }
.lead { color: var(--hint); font-size: 14px; margin: 0 0 18px; }
p.hint, .hint { color: var(--hint); font-size: 13px; line-height: 1.4; }
.section-note { color: var(--hint); font-size: 13px; line-height: 1.4; margin: 8px 4px 0; }
a { color: var(--link); }

.card { background: var(--surface); border-radius: var(--radius); padding: 14px 16px; margin-bottom: 10px; }
.card > * + * { margin-top: 12px; }
.row { display: flex; align-items: center; gap: 10px; }
.grow { flex: 1; min-width: 0; }
.ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.empty { text-align: center; color: var(--hint); padding: 48px 16px; line-height: 1.5;
         background: var(--surface); border-radius: var(--radius); }
.empty .big { font-size: 40px; display: block; margin-bottom: 10px; }

/* ── Buttons ─────────────────────────────────────────────────────────────── */
button { font: inherit; color: inherit; border: none; background: none; cursor: pointer; }
.btn {
  display: flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; width: 100%; padding: 10px 16px;
  background: var(--btn); color: var(--btn-text); border-radius: var(--radius-sm);
  font-weight: 600; transition: opacity .15s, transform .1s;
}
.btn:active { opacity: .75; transform: scale(.99); }
.btn.secondary { background: var(--tint); color: var(--btn); }
.btn.small { width: auto; min-height: 34px; padding: 6px 12px; font-size: 14px; }
.btn:disabled { opacity: .45; }
.icon-btn { background: var(--tint); color: var(--btn); border-radius: 9px; min-width: 32px; height: 32px;
            padding: 0 8px; font-size: 15px; line-height: 1; display: inline-grid; place-items: center; }
.icon-btn:disabled { opacity: .35; }
.actions { display: flex; gap: 8px; }
.actions > * { flex: 1; }

/* ── Grouped lists (files, results) ──────────────────────────────────────── */
.list { background: var(--surface); border-radius: var(--radius); overflow: hidden; }
.list > .file + .file { border-top: 1px solid var(--divider); }
.file { display: flex; align-items: center; gap: 12px; padding: 11px 14px; min-height: 60px;
        transition: background .15s; }
.file[role=button] { cursor: pointer; }
.file[role=button]:active { background: var(--track); }
.file.selected { background: var(--tint); }
.file .ico { width: 38px; height: 38px; flex: none; border-radius: 10px; display: grid; place-items: center;
             font-size: 20px; background: var(--fill); }
.file .name { font-weight: 500; }
.file .meta { font-size: 12.5px; color: var(--hint); margin-top: 2px; }
.file .check { width: 24px; height: 24px; border-radius: 50%; border: 2px solid var(--divider); flex: none;
               display: grid; place-items: center; font-size: 12px; font-weight: 700; color: var(--btn-text);
               transition: background .15s, border-color .15s; }
.file.selected .check { background: var(--btn); border-color: var(--btn); }
.list.standalone .file { border-radius: 0; }

/* ── Tool grid ───────────────────────────────────────────────────────────── */
.tools { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.tool { background: var(--surface); border-radius: var(--radius); padding: 14px 6px 12px; text-align: center;
        font-size: 13px; font-weight: 500; line-height: 1.2; transition: transform .1s; }
.tool:active { transform: scale(.97); }
.tool .em { width: 42px; height: 42px; margin: 0 auto 8px; border-radius: 12px; background: var(--fill);
            display: grid; place-items: center; font-size: 21px; }
.tool small { display: block; color: var(--hint); font-size: 11.5px; font-weight: 400; margin-top: 4px; }
.pick-hint { display: flex; align-items: center; gap: 10px; padding: 14px 16px; background: var(--surface);
             border-radius: var(--radius); color: var(--hint); font-size: 14px; }

/* ── Forms ───────────────────────────────────────────────────────────────── */
label.field, div.field { display: block; margin-bottom: 14px; }
label.field > span, div.field > .label { display: block; font-size: 13px; color: var(--hint); margin: 0 4px 6px; }
input[type=text], input[type=password], input[type=number], textarea, select {
  width: 100%; min-height: 44px; font: inherit; color: var(--text); background: var(--surface);
  border: 1.5px solid transparent; border-radius: var(--radius-sm); padding: 10px 12px; outline: none;
  -webkit-appearance: none; appearance: none;
}
select { background-image: linear-gradient(45deg, transparent 50%, var(--hint) 50%), linear-gradient(135deg, var(--hint) 50%, transparent 50%);
         background-position: calc(100% - 18px) 50%, calc(100% - 13px) 50%; background-size: 5px 5px; background-repeat: no-repeat;
         padding-right: 34px; }
.card input[type=text], .card input[type=password], .card input[type=number], .card textarea, .card select,
#modal input[type=text], #modal textarea { background-color: var(--fill); }
input:focus, textarea:focus, select:focus { border-color: var(--btn); }
input[type=range] { width: 100%; accent-color: var(--btn); }
input[type=color] { width: 44px; height: 34px; border: none; background: none; }
.check-line { display: flex; gap: 12px; align-items: center; padding: 12px 14px; background: var(--surface);
              border-radius: var(--radius-sm); margin-bottom: 8px; }
.check-line input { width: 20px; height: 20px; accent-color: var(--btn); flex: none; }
.range-head { display: flex; justify-content: space-between; color: var(--hint); font-size: 13px; margin: 0 4px 6px; }
.range-head b { color: var(--text); font-weight: 600; }

.seg { display: flex; background: var(--track); border-radius: var(--radius-sm); padding: 3px; gap: 2px; margin-bottom: 14px; flex-wrap: wrap; }
.seg button { flex: 1; min-height: 36px; padding: 7px 8px; border-radius: 8px; font-size: 14px; white-space: nowrap; color: var(--text); }
.seg button.on { background: var(--surface); font-weight: 600; box-shadow: 0 1px 3px rgba(0, 0, 0, .12); }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 14px; }
.chip { background: var(--surface); border-radius: 18px; padding: 7px 13px; font-size: 13.5px; }
.card .chip { background: var(--fill); }
.chip.on { background: var(--btn); color: var(--btn-text); }

.posgrid { display: grid; grid-template-columns: repeat(3, 46px); gap: 5px; padding: 7px;
           background: var(--surface); border-radius: var(--radius-sm); width: max-content; margin-bottom: 14px; }
.posgrid button { height: 34px; border-radius: 7px; background: var(--fill); }
.posgrid button.on { background: var(--btn); }

/* ── Page thumbnails ─────────────────────────────────────────────────────── */
.pages { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 14px 12px; padding: 4px; }
.pg { position: relative; text-align: center; font-size: 12px; color: var(--hint); }
.pg .thumb { position: relative; background: #fff; border-radius: 6px; overflow: hidden; margin-bottom: 5px;
             box-shadow: 0 1px 4px rgba(0, 0, 0, .18); outline: 3px solid transparent; outline-offset: 1px;
             transition: transform .15s, outline-color .15s; }
.pg .thumb img { display: block; width: 100%; height: auto; }
.pg.on .thumb { outline-color: var(--btn); }
.pg.on { color: var(--btn); font-weight: 600; }
.pg.off .thumb { opacity: .35; }
.pg .tag { position: absolute; top: 4px; left: 4px; background: var(--btn); color: var(--btn-text);
           border-radius: 10px; font-size: 11px; padding: 1px 6px; }
.pg .tools-row { display: flex; justify-content: center; gap: 6px; margin-top: 4px; }
.pg .blank { aspect-ratio: 1 / 1.414; display: grid; place-items: center; }
.pg .file-tag { position: absolute; bottom: 4px; right: 4px; background: rgba(0, 0, 0, .55); color: #fff;
                border-radius: 8px; font-size: 10px; padding: 1px 5px; }
.scissors { font-size: 18px; opacity: .3; }
.scissors.on { opacity: 1; }
.sortable-ghost { opacity: .3; }
.select-bar { display: flex; align-items: center; gap: 8px; margin: 0 0 12px; }
.select-bar .grow { color: var(--hint); font-size: 13px; margin-left: 4px; }

/* ── Single page editor ──────────────────────────────────────────────────── */
.editor { position: relative; margin: 0 auto 12px; touch-action: none; background: #fff;
          box-shadow: 0 2px 10px rgba(0, 0, 0, .18); border-radius: 4px; overflow: hidden; }
.editor img { display: block; width: 100%; height: 100%; pointer-events: none; }
.editor .layer { position: absolute; inset: 0; }
.item { position: absolute; outline: 1px dashed transparent; touch-action: none; }
.item.sel { outline-color: var(--btn); }
.item img { width: 100%; height: 100%; pointer-events: none; }
.item .txt { white-space: pre; line-height: 1.15; pointer-events: none; }
.item .handle { position: absolute; right: -9px; bottom: -9px; width: 18px; height: 18px; border-radius: 50%;
                background: var(--btn); border: 2px solid #fff; display: none; }
.item.sel .handle { display: block; }
.redact-box { position: absolute; background: rgba(0, 0, 0, .85); }
.redact-box.sel { outline: 2px solid var(--btn); }
.crop-box { position: absolute; border: 2px solid var(--btn); box-shadow: 0 0 0 9999px rgba(0, 0, 0, .45); }
.crop-box .h { position: absolute; width: 22px; height: 22px; background: var(--btn); border-radius: 50%; border: 3px solid #fff; }
.pager { display: flex; justify-content: center; align-items: center; gap: 14px; margin-bottom: 12px; }
.pager .hint { min-width: 92px; text-align: center; }
.toolbar { display: flex; gap: 6px; overflow-x: auto; padding: 2px 0 10px; margin-bottom: 4px; scrollbar-width: none; }
.toolbar::-webkit-scrollbar { display: none; }
.toolbar button { flex: none; background: var(--surface); border-radius: 18px; padding: 8px 13px; font-size: 14px; }
.toolbar button:active { background: var(--tint); }
.toolbar button.on { background: var(--btn); color: var(--btn-text); }

/* Signature pad */
.pad { width: 100%; height: 180px; background: #fff; border-radius: var(--radius-sm); touch-action: none; display: block;
       box-shadow: inset 0 0 0 1px rgba(0, 0, 0, .08); }

/* ── Tab bar ─────────────────────────────────────────────────────────────── */
#tabs { position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
        padding: 8px 16px calc(8px + env(safe-area-inset-bottom));
        background: var(--page); border-top: 1px solid var(--divider); }
#tabs .track { display: flex; gap: 2px; padding: 3px; background: var(--track); border-radius: 12px; }
#tabs button { flex: 1; min-height: 38px; border-radius: 9px; font-size: 14px; font-weight: 500; color: var(--hint);
               transition: background .15s, color .15s; }
#tabs button.active { background: var(--surface); color: var(--text); font-weight: 600;
                      box-shadow: 0 1px 3px rgba(0, 0, 0, .12); }
body.tool-open #tabs { display: none; }
body.tool-open { padding-bottom: 28px; }

/* ── Modal sheet, toast, spinner ─────────────────────────────────────────── */
#modal { position: fixed; inset: 0; background: rgba(0, 0, 0, .45); display: flex; align-items: flex-end; z-index: 20; }
#modal[hidden] { display: none; }
#modal .sheet { background: var(--surface); width: 100%; border-radius: 18px 18px 0 0;
                padding: 10px 16px calc(18px + env(safe-area-inset-bottom)); max-height: 85vh; overflow: auto; }
#modal .sheet::before { content: ""; display: block; width: 36px; height: 5px; border-radius: 3px;
                        background: var(--divider); margin: 0 auto 14px; }
#modal .sheet h1 { margin-bottom: 14px; }
#modal .pkg { background: var(--fill); }

#toast { position: fixed; left: 16px; right: 16px; bottom: 96px; background: rgba(28, 28, 30, .94); color: #fff;
         padding: 12px 16px; border-radius: 12px; font-size: 14px; z-index: 30; text-align: center;
         box-shadow: 0 6px 20px rgba(0, 0, 0, .25); }
body.tool-open #toast { bottom: 24px; }
.spinner { width: 28px; height: 28px; border: 3px solid var(--track); border-top-color: var(--btn); border-radius: 50%;
           animation: spin .8s linear infinite; margin: 36px auto; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Account ─────────────────────────────────────────────────────────────── */
.stats { display: grid; grid-template-columns: 1fr 1fr; }
.stats > div + div { border-left: 1px solid var(--divider); padding-left: 16px; }
.stat { font-size: 30px; font-weight: 700; line-height: 1.1; letter-spacing: -.5px; }
.stat-label { color: var(--hint); font-size: 13px; margin-top: 3px; }
.pkg-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.pkg { background: var(--surface); border-radius: var(--radius); padding: 14px; text-align: left;
       display: flex; flex-direction: column; gap: 8px; transition: transform .1s; }
.pkg:active { transform: scale(.98); }
.pkg b { font-size: 17px; font-weight: 700; }
.pkg .price { align-self: flex-start; background: var(--tint); color: var(--btn); font-weight: 600; font-size: 13.5px;
              border-radius: 8px; padding: 3px 9px; }
.linkbox { display: flex; align-items: center; gap: 8px; background: var(--fill); border-radius: var(--radius-sm);
           padding: 6px 6px 6px 12px; }
.linkbox span { flex: 1; min-width: 0; font-size: 13.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.redeem { display: flex; gap: 8px; }
.redeem input { flex: 1; }
.redeem .btn { width: auto; flex: none; }
.divider { height: 1px; background: var(--divider); margin: 14px 0; }

/* Files tab: newest files sit right above the tools, older ones behind a button */
.section-note.top { margin: -2px 4px 10px; }
.show-older { margin-bottom: 8px; min-height: 38px; font-size: 14px; }
.below { min-height: 50vh; }
