:root{--calc:#0e7c86;--calcbg:rgba(14,124,134,.055);--ui:1;--fg:#1f2328;--mut:#6b7280;--line:#e3e5e8;--hd:#f6f7f9;--acc:#1a73e8;--accbg:rgba(26,115,232,.09);--bg:#fff;--st:#7c3aed;--err:#d93025;font-size:calc(14px*var(--ui))}
*{box-sizing:border-box}
html,body{margin:0;height:100%;overflow:hidden;overflow-x:hidden;width:100%;max-width:100vw;overscroll-behavior:none;background:var(--bg);color:var(--fg);font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Inter,sans-serif;-webkit-text-size-adjust:100%}
button{font:inherit;color:inherit;background:none;border:0;cursor:pointer;-webkit-tap-highlight-color:transparent}
svg{width:1.3em;height:1.3em;fill:none;stroke:currentColor;stroke-width:1.8;stroke-linecap:round;stroke-linejoin:round}
#app{position:fixed;inset:0;display:flex;flex-direction:column;max-width:100vw;overflow:hidden;padding:env(safe-area-inset-top) env(safe-area-inset-right) 0 env(safe-area-inset-left)}
#top{display:flex;align-items:center;gap:.35rem;padding:.35rem .6rem;border-bottom:1px solid var(--line);min-height:2.9rem;min-width:0;max-width:100%;overflow:hidden}
#crumbs{flex:1;min-width:0;display:flex;align-items:center;gap:.15rem;overflow:hidden;white-space:nowrap}
#crumbs .back{width:2.2rem;height:2.2rem;border-radius:.6rem;font-size:1.5rem;line-height:1;color:var(--acc);flex:none}
#crumbs .back:hover{background:var(--hd)}
#crumbs .cr{padding:.3rem .45rem;border-radius:.45rem;font-weight:500;overflow:hidden;text-overflow:ellipsis;min-width:0;flex:0 1 auto}
#crumbs .cr.link{color:var(--mut)} #crumbs .cr.link:hover{background:var(--hd);color:var(--fg)}
#crumbs .cr.cur{font-weight:650;flex:0 1 auto}
#crumbs .cr.link{flex-shrink:0;max-width:7.5em}
#crumbs .sep{color:#c2c6cc;flex:none}
#top-l,#top-r{display:flex;align-items:center;gap:.05rem;flex:none}
.ib{height:2.2rem;min-width:2.2rem;border-radius:.6rem;display:inline-flex;align-items:center;justify-content:center;color:#3c4043;padding:0 .4rem}
.ib:hover{background:var(--hd)} .ib:disabled{opacity:.35;cursor:default;background:none}
.kbd{font-size:.78rem;border:1px solid var(--line);border-radius:.35rem;padding:.1rem .35rem;color:var(--mut)}
#save-st{font-size:.78rem;color:var(--mut);margin-right:.3rem}
#fbar{display:flex;align-items:center;border-bottom:1px solid var(--line);height:2.3rem;font-size:.95rem}
#fbar-l{display:flex;align-items:stretch;flex:none;height:100%;border-right:1px solid var(--line);max-width:45%;min-width:0}
#fbar-l .fb{width:2.1rem;flex:none;display:inline-flex;align-items:center;justify-content:center;color:#3c4043;border-right:1px solid var(--line)}
#fbar-l .fb svg{width:1.15rem;height:1.15rem}
#fbar-l .fb:hover{background:var(--hd)} #fbar-l .fb:disabled{opacity:.35;cursor:default;background:none}
#crumbs .pos{color:var(--mut);font-variant-numeric:tabular-nums;flex:none;max-width:10em;overflow:hidden;text-overflow:ellipsis}#b-up.kf{outline:2px solid var(--acc);outline-offset:-2px}
#fx{flex:1;min-width:0;position:relative;height:100%}
#fx-in,#fx-view{position:absolute;inset:0;border:0;outline:0;padding:0 .7rem;font:inherit;font-family:ui-monospace,SFMono-Regular,Menlo,monospace;font-size:.9rem;background:transparent;color:var(--fg);white-space:pre;overflow:hidden;display:flex;align-items:center}
#fx-view{display:none}
#scroller{flex:1;min-height:0;overflow:auto;position:relative;touch-action:manipulation;overscroll-behavior:contain;-webkit-overflow-scrolling:touch;background:#fff}
#sheet{position:relative;width:max-content;transform-origin:0 0}
#grid{border-collapse:separate;border-spacing:0;table-layout:fixed;font-size:calc(13px*var(--z,1));user-select:none;-webkit-user-select:none}
#grid th,#grid td{height:calc(26px*var(--z,1));line-height:calc(26px*var(--z,1) - 1px);padding:0 calc(6px*var(--z,1));border-right:1px solid var(--line);border-bottom:1px solid var(--line);overflow:hidden;white-space:nowrap;text-overflow:clip;max-height:calc(26px*var(--z,1))}
#grid th{background:var(--hd);color:var(--mut);font-weight:500;font-size:.85em;text-align:center;position:sticky;z-index:3}
/* field headers above selection handles (z-index 5) */
#grid thead th{top:0;z-index:6} #grid tbody th{left:0;z-index:2} #grid thead th.corner{left:0;z-index:7}
/* pinch uses live --z layout (no transform), so sticky headers stay put */
#grid th.on{background:#e8f0fe;color:var(--acc);font-weight:600}
#grid td.n{text-align:right;font-variant-numeric:tabular-nums}
#grid td.e{color:var(--err);text-align:center;font-size:.9em}
#grid td.b{text-align:center}
#grid td.s{padding:0 calc(3px*var(--z,1));vertical-align:middle}
.chip{display:flex;align-items:center;gap:.35em;height:calc(20px*var(--z,1));line-height:calc(20px*var(--z,1));border-radius:calc(5px*var(--z,1));background:#f3efff;color:var(--st);padding:0 .45em;font-size:.92em;font-weight:550;overflow:hidden}
.chip i{font-style:normal;opacity:.65;font-weight:450}
#grid td.inr{background:var(--accbg)}
#grid :is(td,th).spare{background:0;color:#999;border-right:1px dashed #ddd;border-bottom:1px dashed #ddd}
#grid th.spare,#grid th.spare .fname{font-style:italic;font-weight:400;color:#999}
#grid :is(td,th).spare:hover,#grid th.spare.on{background:var(--accbg)}
#selbox,#selhd{position:absolute;border:2px solid var(--acc);pointer-events:none;z-index:1;display:none;border-radius:1px}
#selbox.range,#selhd.range{background:var(--accbg)}
#selhd{z-index:8}
#pointbox{position:absolute;border:2px dashed #e37400;background:rgba(227,116,0,.07);pointer-events:none;display:none;z-index:1}
.hdl{position:absolute;width:30px;height:30px;margin:-15px 0 0 -15px;display:none;z-index:5;touch-action:none}
.hdl::after{content:"";position:absolute;left:9px;top:9px;width:12px;height:12px;border-radius:50%;background:#fff;border:2.5px solid var(--acc);box-sizing:border-box}
body.touch .hdl.show{display:block}
#cell-ed{position:absolute;display:none;z-index:6;border:2px solid var(--acc);outline:0;padding:0 calc(5px*var(--z,1));font:inherit;font-size:calc(13px*var(--z,1));background:#fff;box-shadow:0 2px 8px rgba(0,0,0,.15);min-width:0}
#cell-view{position:absolute;display:none;z-index:6;border:2px solid var(--acc);padding:calc(1px*var(--z,1)) calc(5px*var(--z,1));font-size:calc(13px*var(--z,1));line-height:calc(22px*var(--z,1));background:#fff;box-shadow:0 2px 8px rgba(0,0,0,.15);white-space:pre-wrap;word-break:break-all;overflow:hidden}
#fx-view .fxs{white-space:pre;flex:none}
.caret{display:inline-block;width:2px;height:1.15em;background:var(--acc);vertical-align:middle;margin:0 -1px;animation:blink 1s steps(1) infinite}
@keyframes blink{50%{opacity:0}}
.tk-ref{color:#1967d2}.tk-fn{color:#7c3aed}.tk-num{color:#0b8043}.tk-str{color:#b06000}
#bbar{display:none;flex-direction:column;border-top:1px solid var(--line);padding:.1rem .2rem calc(.1rem + env(safe-area-inset-bottom));background:#fff;overflow:hidden;min-width:0;max-width:100%;gap:.05rem}
#bbar .brow{display:flex;flex:none;width:100%;gap:0}
#bbar button{flex:1 1 0;min-width:0;display:flex;flex-direction:column;align-items:center;gap:.05rem;padding:.28rem 0;border-radius:.5rem;font-size:.68rem;color:#3c4043;line-height:1.05;background:0;border:0}
#bbar button span{max-width:100%;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
#bbar button:active{background:var(--hd)} #bbar button:disabled{opacity:.35}
#bbar svg{width:1.35rem;height:1.35rem;flex:none}
/* keypad */
#kpad{background:#eef0f3;border-top:1px solid #d9dce1;padding:.3rem .25rem calc(.3rem + env(safe-area-inset-bottom));user-select:none;-webkit-user-select:none;touch-action:manipulation}
#kpad .row{display:flex;gap:.25rem;margin-bottom:.25rem}
#kpad .row:last-child{margin-bottom:0}
#kpad button{flex:1 1 0;min-width:0;height:2.85rem;height:clamp(1.55rem,calc((100vh - 15rem) / 6),2.85rem);height:clamp(1.55rem,calc((100dvh - 15rem) / 6),2.85rem);border-radius:.5rem;background:#fff;box-shadow:0 1px 0 rgba(0,0,0,.18);font-size:1.15rem;display:flex;align-items:center;justify-content:center;color:var(--fg)}
#kpad button.op{background:#e2e5ea;font-size:1.2rem}
#kpad button.fn{background:#e9e3fb;color:var(--st);font-size:.95rem;font-weight:600}
#kpad button.ent{background:var(--acc);color:#fff}
#kpad button.mod{background:#d3d7de;font-size:.9rem}
#kpad button.eq{background:#d6e4fd;color:var(--acc);font-size:1.35rem;font-weight:600}
#kpad button.dot{background:#e2e5ea;font-size:1.2rem;font-weight:700}
#kpad button.on{background:#c7d8fb}
#kpad button:active,#kpad button.pr{filter:brightness(.88)}
#kpad .abc button{font-size:1.1rem}
#kpad .corner{flex:0 0 17%} #kpad .w15{flex-grow:1.5} #kpad .w2{flex-grow:2} #kpad .half{flex-grow:.5;visibility:hidden}
/* function panel */
#fnpanel{position:fixed;left:0;right:0;z-index:20;background:#fff;border-top:1px solid var(--line);box-shadow:0 -6px 20px rgba(0,0,0,.08);display:flex;flex-direction:column}
#fnpanel .fp-head{display:flex;align-items:center;gap:.4rem;padding:.5rem .6rem;border-bottom:1px solid var(--line)}
#fnpanel .fp-q{flex:1;height:2.2rem;border-radius:.55rem;background:var(--hd);display:flex;align-items:center;padding:0 .7rem;color:var(--fg);white-space:pre;overflow:hidden}
#fnpanel .fp-q.ph{color:var(--mut)}
#fnpanel .cats{display:flex;flex-wrap:wrap;gap:.3rem;padding:.45rem .6rem;border-bottom:1px solid var(--line);flex:none}
#fnpanel .cats button{padding:.3rem .7rem;border-radius:1rem;background:var(--hd);font-size:.85rem;white-space:nowrap}
#fnpanel .cats button.on{background:var(--acc);color:#fff}
#fnpanel .fl{overflow-y:auto;flex:1}
#fnpanel .fi{display:flex;flex-direction:column;align-items:flex-start;width:100%;padding:.55rem .8rem;border-bottom:1px solid #f0f1f3;text-align:left}
#fnpanel .fi b{font-family:ui-monospace,Menlo,monospace;font-weight:600;font-size:.9rem}
#fnpanel .fi span{font-size:.8rem;color:var(--mut)}
/* palette, menu, dialogs */
#pal,#dlg{position:fixed;inset:0;background:rgba(15,23,42,.28);z-index:40;display:flex;align-items:flex-start;justify-content:center;padding-top:12vh;padding-top:12dvh}
#pal[hidden],#dlg[hidden],#menu[hidden],#fnpanel[hidden],#kpad[hidden]{display:none}
.pal-card,.dlg-card{width:min(560px,94vw);background:#fff;border-radius:.9rem;box-shadow:0 20px 60px rgba(0,0,0,.25);overflow:hidden;display:flex;flex-direction:column;max-height:72vh;max-height:72dvh}
.pal-head{border-bottom:1px solid var(--line)}
#pal-q{width:100%;border:0;outline:0;padding:.95rem 1.1rem;font:inherit;font-size:1.05rem}
#pal-sz{display:flex;gap:.6rem;align-items:center;padding:.5rem 1rem;border-bottom:1px solid var(--line);font-size:.82rem;color:var(--mut);flex-wrap:wrap}
.seg{display:inline-flex;border:1px solid var(--line);border-radius:.5rem;overflow:hidden}
.seg button{padding:.25rem .6rem;font-size:.82rem;color:var(--fg)} .seg button.on{background:var(--acc);color:#fff}
.seg span{padding:.25rem .5rem;min-width:3.2rem;text-align:center;color:var(--fg);border-left:1px solid var(--line);border-right:1px solid var(--line)}
#pal-list{overflow-y:auto;padding:.3rem}
.pi{display:flex;align-items:center;width:100%;padding:.6rem .8rem;border-radius:.5rem;text-align:left;gap:.6rem}
.pi.on{background:var(--accbg)} .pi .k{margin-left:auto;font-size:.78rem;color:var(--mut)} .pi .g{font-size:.72rem;color:var(--mut);width:4.5rem;flex:none}
body.touch .pi .k{display:none}
#menu-back{position:fixed;inset:0;background:rgba(15,23,42,.28);z-index:44;display:none}
#menu-back.on{display:block}
#menu{position:fixed;z-index:45;background:#fff;border-radius:.6rem;box-shadow:0 10px 30px rgba(0,0,0,.2);padding:.3rem;min-width:13rem;max-height:70vh;overflow:auto}
#menu .m-close{position:absolute;right:.4rem;top:.35rem;width:2rem;height:2rem;border:0;background:0;font-size:1.2rem;color:var(--mut);border-radius:.45rem}
#menu .m-close:hover{background:var(--hd)}
#menu .m-grab{display:none;width:2.4rem;height:.25rem;background:#d0d5dd;border-radius:99px;margin:.2rem auto .35rem}
#menu .pi{padding:.45rem .7rem;font-size:.92rem;display:block;width:100%;text-align:left;background:0;border:0;cursor:pointer} #menu .pi:hover{background:var(--hd)} #menu hr{border:0;border-top:1px solid var(--line);margin:.25rem}
#menu .mh{padding:.35rem .7rem;font-size:.78rem;font-weight:600;color:var(--muted);letter-spacing:.02em}
#menu.sheet{left:0!important;right:0;bottom:0;top:auto;border-radius:.9rem .9rem 0 0;max-height:65vh;padding:.15rem .3rem calc(.5rem + env(safe-area-inset-bottom));box-shadow:0 -8px 30px rgba(0,0,0,.18)}
#menu.sheet .m-grab{display:block}
.dlg-card{padding:1.2rem;gap:.7rem}
.dlg-card h2{margin:0;font-size:1.1rem}
.dlg-card input{font:inherit;padding:.6rem .7rem;border:1px solid var(--line);border-radius:.5rem;width:100%;font-size:16px}
.dlg-card .row{display:flex;gap:.5rem;justify-content:flex-end}
.btn{padding:.5rem 1rem;border-radius:.55rem;background:var(--hd)} .btn.pri{background:var(--acc);color:#fff}
.dlg-card .list{overflow-y:auto;max-height:45vh;max-height:45dvh;display:flex;flex-direction:column}
.dlg-card .list button{text-align:left;padding:.55rem .6rem;border-radius:.45rem;display:flex;justify-content:space-between}
.dlg-card .list button:hover{background:var(--hd)} .dlg-card .muted{color:var(--mut);font-size:.85rem}
.dlg-card table{border-collapse:collapse;font-size:.88rem} .dlg-card td{padding:.25rem .5rem;border-bottom:1px solid #f0f1f3} .dlg-card td:first-child{white-space:nowrap;color:var(--mut)}
#toast{position:fixed;left:50%;bottom:5rem;transform:translateX(-50%) translateY(20px);background:#202124;color:#fff;padding:.55rem 1rem;border-radius:.6rem;font-size:.9rem;opacity:0;pointer-events:none;transition:.2s;z-index:60;max-width:90vw}
#toast.on{opacity:1;transform:translateX(-50%)}
/* phone */
body.touch #bbar{display:flex}
body.touch .dk{display:none}
body.touch #fx-in{display:none} body.touch #fx-view{display:flex}
body.touch.editing #bbar{display:none}
body.touch #save-st{display:none}
@media (max-width:700px){#top{padding:.3rem .35rem}.ib .kbd{border:0;font-size:.95rem;color:#3c4043}}
@media (max-width:700px){#bbar{display:flex}.dk{display:none}body.editing #bbar{display:none}}

/* iOS zooms the page when focusing inputs under 16px */
body.touch #pal-q,body.touch .dlg-card input{font-size:16px}
/* custom-keypad text fields (phone): no OS keyboard */
body.touch .kf-in{display:none}
.kf-view{white-space:pre;overflow:hidden;cursor:text;-webkit-user-select:none;user-select:none;min-height:1.4em}
.kf-view.ph{color:var(--mut)}
.kf-view .kf-all{background:#c7d8fb;border-radius:2px}
.pal-head .kf-view{padding:.95rem 1.1rem;font-size:1.05rem;line-height:1.4}
.dlg-card .kf-view{padding:.6rem .7rem;border:1px solid var(--line);border-radius:.5rem;font-size:16px;line-height:1.4}
body.kf .dlg-card .kf-view,body.kf .pal-head .kf-view{box-shadow:inset 0 0 0 2px var(--acc)}
body.kf #kpad{position:fixed;left:0;right:0;bottom:0;z-index:50}
body.kf #pal,body.kf #dlg{padding-top:3vh;padding-top:3dvh}
#kpad button.off{opacity:.35}
#kpad button.cancel{color:var(--err);font-weight:700;font-size:.95rem;letter-spacing:.02em}
#kpad button.fwd{font-size:1.15rem}#kpad button.mir{transform:scaleX(-1)}

/* AI generate */
#ai-empty{position:absolute;left:50%;top:38%;transform:translateX(-50%);z-index:6;display:flex;align-items:center;gap:.45rem;padding:.65rem 1.1rem;border-radius:999px;background:#fff;color:var(--acc);font-weight:600;box-shadow:0 4px 18px rgba(26,115,232,.22),0 0 0 1px rgba(26,115,232,.25);max-width:90vw}
#ai-empty[hidden],body.kf #ai-empty{display:none}
#ai-empty svg{width:18px;height:18px;fill:currentColor;stroke:none}
.chip{padding:.35rem .7rem;border-radius:999px;background:var(--hd);font-size:.85rem;max-width:100%;text-align:left}
.linkb{color:var(--acc);text-decoration:underline;padding:0 .2rem}
.spin{display:inline-block;width:.9em;height:.9em;margin-right:.45rem;vertical-align:-.12em;border:2px solid #c7d8fb;border-top-color:var(--acc);border-radius:50%;animation:spin .8s linear infinite}
@keyframes spin{to{transform:rotate(360deg)}}

/* account / profile menu */
.acct-card .acct-email{font-weight:600;word-break:break-all}
.acct-card .acct-sec{margin-top:.55rem;padding-top:.55rem;border-top:1px solid var(--line)}
.acct-card .acct-sec h3{margin:0 0 .35rem;font-size:.78rem;text-transform:uppercase;letter-spacing:.04em;color:var(--mut);font-weight:600}
.acct-card .acct-row{display:flex;align-items:center;justify-content:space-between;gap:.5rem;padding:.25rem 0;font-size:.9rem}
.acct-card .list{margin-top:.35rem;max-height:32vh;max-height:32dvh}

/* table chrome: field-name headers + transpose in the corner cell */
#grid th.corner{padding:0;overflow:visible}
#grid th .tr-btn{width:100%;height:100%;min-height:calc(26px*var(--z,1));display:flex;align-items:center;justify-content:center;color:var(--mut);border-radius:0}
#grid th .tr-btn svg{width:1em;height:1em}
#grid th .tr-btn.on,#grid th .tr-btn:hover,#grid th .tr-btn:active{background:#e8f0fe;color:var(--acc)}
#grid th.field{font-weight:600;color:var(--fg);cursor:pointer;max-width:100%;overflow:hidden;text-overflow:ellipsis}
#grid tbody th.field{text-align:left;padding-right:1.1em}
#grid th.field.dflt{font-weight:500;color:var(--mut)} /* a grid field not renamed yet: its letter */
#grid th.field .fname{display:block;overflow:hidden;text-overflow:ellipsis;padding:0 .15em}
#grid tr.filtered{display:none} #grid tr.vsp td{padding:0;border:0;max-height:none;line-height:0}
#grid th .fh-menu{position:absolute;right:2px;top:50%;transform:translateY(-50%);font-size:.7em;opacity:.55;padding:0 .2em}

.wb-row{display:flex;align-items:stretch;gap:.25rem;border-bottom:1px solid var(--line)}
.wb-row .wb-open{flex:1;display:flex;justify-content:space-between;gap:.5rem;padding:.7rem .85rem;text-align:left;background:0;border:0;font:inherit;cursor:pointer}
.wb-row .wb-open:hover{background:var(--hd)}
.wb-row .wb-del{flex:none;width:2.6rem;border:0;background:0;color:#c5221f;font-size:1.1rem;cursor:pointer;border-radius:.45rem;display:flex;align-items:center;justify-content:center}
.wb-row .wb-del svg{width:1.15rem;height:1.15rem;display:block;pointer-events:none}
.wb-row .wb-del:hover{background:#fce8e6}
.acct-wh{display:flex;align-items:center;justify-content:space-between;gap:.5rem}
.acct-wh h3{margin:0}
.dlg-card .list .wb-row:last-child{border-bottom:0}
#pal-sz .seg button[data-zreset],#cl-z{min-width:3.2rem}

/* ficarra-polish: toolbar focus and touch size. Grid, keypad, and selection metrics stay as shipped. */
#top { min-height: 44px; }
.ib, #crumbs .back {
  min-width: 44px;
  min-height: 44px;
}
#top-l .ib { width: 44px; height: 44px; }
#fbar { height: 44px; }
#fbar-l .fb { width: 44px; min-height: 44px; }
#bbar button { min-height: 44px; }
button:focus-visible, a:focus-visible, .ib:focus-visible, #fbar-l .fb:focus-visible {
  outline: 2px solid var(--acc);
  outline-offset: 2px;
}
#fx-in:focus, #cell-ed:focus, #pal-q:focus { outline: none; }
@media (prefers-reduced-motion: reduce) {
  .caret, .spin { animation: none; }
}
#viz{display:none}body.viz #viz{display:block}body.viz #sheet{display:none}

/* calculated fields: accent header text, faint cell tint (explicit fills win: no class then); selection overlays sit above */
#grid th.calc .fname,.vz-l.calc{color:var(--calc)}#grid td.cf{background:var(--calcbg)}
