:root {
  --brand: #4c216d;
  --brand-dark: #2f1543;
  --brand-light: #efe7f5;
  --excel-green: #107c41;
  --ui-font: 'Segoe UI', system-ui, -apple-system, Roboto, Arial, sans-serif;
  --cell-font: Calibri, Carlito, 'Segoe UI', Arial, sans-serif;
  --line: #e1dfdd;
  --muted: #605e5c;
  --ok: #107c41;
  --warn: #9a6700;
  --err: #c42b1c;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: var(--ui-font);
  font-size: 14px;
  color: #201f1e;
  background: #fff;
  display: grid;
  grid-template-rows: auto 1fr auto;
  overflow: hidden;
}
button, input, select { font: inherit; }
[hidden] { display: none !important; }

/* ------------------------------------------------------------ barra superior */
.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 12px;
  height: 46px;
  background: var(--brand-dark);
  color: #fff;
  box-shadow: 0 1px 0 rgba(0, 0, 0, .25);
  z-index: 30;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 600; white-space: nowrap; }
.tools { display: flex; align-items: center; gap: 4px; flex: 1; min-width: 0; }
.tb {
  display: inline-flex; align-items: center; gap: 6px;
  height: 32px; padding: 0 10px;
  color: #fff; background: transparent; border: 0; border-radius: 4px; cursor: pointer;
  white-space: nowrap;
}
.tb:hover { background: rgba(255, 255, 255, .12); }
.tb:focus-visible, .user-btn:focus-visible { outline: 2px solid #fff; outline-offset: -2px; }
.sheet-picker {
  display: inline-flex; flex-direction: row; align-items: center; gap: 6px;
  color: #fff; font-size: 14px;
  height: 32px; padding: 0 4px 0 10px; margin: 0 4px;
  background: rgba(255, 255, 255, .1); border-radius: 4px;
  min-width: 0;
}
.sheet-picker select {
  background: transparent; color: #fff; border: 0; height: 30px; max-width: 260px;
  cursor: pointer; outline: none;
}
.sheet-picker select option { color: #201f1e; }
.user { position: relative; }
.user-btn {
  display: flex; align-items: center; gap: 8px;
  background: transparent; color: #fff; border: 0; padding: 4px 8px; border-radius: 4px; cursor: pointer;
}
.user-btn:hover { background: rgba(255, 255, 255, .12); }
.avatar {
  width: 28px; height: 28px; border-radius: 50%;
  background: rgba(255, 255, 255, .18); display: grid; place-items: center;
}
.who { display: flex; flex-direction: column; align-items: flex-start; line-height: 1.15; }
.who small { opacity: .75; font-size: 11px; }
.menu {
  position: absolute; right: 0; top: calc(100% + 6px);
  min-width: 240px; padding: 6px 0;
  background: #fff; color: #201f1e; border: 1px solid var(--line); border-radius: 6px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .18);
  display: none;
}
.menu.open { display: block; }
.menu button {
  display: block; width: 100%; text-align: left;
  padding: 8px 16px; background: none; border: 0; cursor: pointer;
}
.menu button:hover { background: #f3f2f1; }
.menu .sep { height: 1px; background: var(--line); margin: 6px 0; }

/* ------------------------------------------------------------ área da planilha */
#viewport {
  position: relative;
  overflow: auto;
  background: #fff;
  outline: none;
}
.sheet-wrap {
  position: relative;
  background: #fff;
  min-height: 100%;
}
table.grid {
  border-collapse: separate;
  border-spacing: 0;
  table-layout: fixed;
  font-family: var(--cell-font);
  font-size: 14.67px;
  color: #000;
  background: #fff;
  user-select: none;
}
table.grid td.c {
  box-sizing: border-box;
  padding: 0 2px;
  overflow: hidden;
  white-space: nowrap;
  vertical-align: bottom;
  line-height: 1.15;
  cursor: cell;
}
table.grid td.c.ovf { overflow: visible; }
table.grid .ci {
  display: flex;
  align-items: center;
  width: 100%;
  min-width: 0;
  white-space: nowrap;
}
table.grid td.c.wrap .ci {
  display: block;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
table.grid td.c.ovf .ci { overflow: visible; }
table.grid td.c.err { color: inherit; }
table.grid td.hasico .ci { position: relative; padding-left: 17px; }
table.grid td.hasico .ico { position: absolute; left: 0; top: 50%; transform: translateY(-50%); display: inline-flex; }
table.grid td.hasico .tv { display: flex; flex: 1; justify-content: inherit; min-width: 0; }
table.grid td.c .ico { display: inline-flex; }
table.grid td.ed:hover { box-shadow: inset 0 0 0 1px rgba(16, 124, 65, .5); }
table.grid td.sel {
  outline: 2px solid var(--excel-green);
  outline-offset: -2px;
}
table.grid td.frz { position: sticky; z-index: 2; }
table.grid thead th {
  position: sticky; top: 0; z-index: 3;
  font: 12px var(--ui-font);
  color: #444;
  background: #f5f5f5;
  border-right: 1px solid #d4d4d4;
  border-bottom: 1px solid #d4d4d4;
  font-weight: 400;
  padding: 0;
}
table.grid th.corner { left: 0; z-index: 4; }
table.grid th.rh {
  position: sticky; left: 0; z-index: 2;
  font: 12px var(--ui-font);
  color: #444;
  background: #f5f5f5;
  border-right: 1px solid #d4d4d4;
  border-bottom: 1px solid #d4d4d4;
  font-weight: 400;
  text-align: center;
  padding: 0;
}
table.grid th.hsel { background: #d3ebdc; color: #0b5a2e; font-weight: 600; }
table.grid thead th.hsel { box-shadow: inset 0 -2px 0 var(--excel-green); }
table.grid th.rh.hsel { box-shadow: inset -2px 0 0 var(--excel-green); }
table.grid tr.spacer td { padding: 0; border: 0; }

.drawings { position: absolute; pointer-events: none; }
.dr { position: absolute; pointer-events: auto; box-sizing: border-box; }
.dr.sp {
  display: flex; flex-direction: column;
  padding: 4.8px 9.6px;
  overflow: hidden;
  font-family: var(--cell-font);
  font-size: 14.67px;
  line-height: 1.2;
}
.dr.sp p { margin: 0; overflow-wrap: anywhere; }
.dr.pic, .picbox .pic { user-select: none; -webkit-user-drag: none; object-fit: fill; }
.picbox { overflow: hidden; }
.picbox .pic { position: absolute; max-width: none; }
.dr.lnk { cursor: pointer; transition: filter .12s, transform .12s; }
.dr.lnk:hover { filter: brightness(1.1) saturate(1.05); }
.dr.lnk:active { transform: translateY(1px); }

.dv-btn {
  position: absolute; z-index: 6;
  width: 17px; height: 17px;
  display: grid; place-items: center;
  background: #f3f3f3; color: #333; border: 1px solid #a6a6a6;
  cursor: pointer;
}
.dv-btn:hover { background: #e6e6e6; }
.dv-list {
  position: absolute; z-index: 25;
  max-height: 260px; overflow: auto;
  background: #fff; border: 1px solid #8a8a8a;
  box-shadow: 0 4px 12px rgba(0, 0, 0, .2);
  font: 14px var(--cell-font);
  padding: 2px 0;
}
.dv-list .opt { padding: 2px 8px; white-space: nowrap; cursor: default; }
.dv-list .opt.on { font-weight: 700; }
.dv-list .opt.hl, .dv-list .opt:hover { background: #cce4f7; }
.dv-list .opt.empty { color: var(--muted); }

.cell-editor {
  position: absolute; z-index: 20;
  outline: 2px solid var(--excel-green);
  background: #fff;
  box-shadow: 0 2px 6px rgba(0, 0, 0, .2);
}
.cell-editor input {
  width: 100%; height: 100%;
  border: 0; outline: none; padding: 0 3px;
  font-family: var(--cell-font); font-size: 14.67px;
  background: #fff;
}

/* ------------------------------------------------------------ barra de status */
.statusbar {
  display: flex; align-items: center; gap: 16px;
  height: 28px; padding: 0 12px;
  font-size: 12px;
  background: #f3f2f1; border-top: 1px solid var(--line);
  color: #3b3a39;
}
#status-text { min-width: 200px; }
#status-text.dirty, #status-text.saving { color: var(--warn); }
#status-text.ok { color: var(--ok); }
#status-text.error { color: var(--err); font-weight: 600; }
#cell-info { flex: 1; display: flex; align-items: center; gap: 10px; min-width: 0; overflow: hidden; white-space: nowrap; }
#cell-info .addr { font-weight: 600; min-width: 42px; }
#cell-info .val { overflow: hidden; text-overflow: ellipsis; }
#cell-info .tag { padding: 0 6px; border-radius: 8px; background: #dff6dd; color: var(--ok); }
.zoom { display: flex; align-items: center; gap: 2px; }
.zoom button {
  height: 22px; min-width: 24px; padding: 0 6px;
  background: transparent; border: 0; border-radius: 3px; cursor: pointer; color: inherit;
}
.zoom button:hover { background: #e1dfdd; }
#zoom-label { min-width: 48px; }

/* ------------------------------------------------------------ carregamento e avisos */
.loading {
  position: fixed; inset: 0; z-index: 100;
  display: none; flex-direction: column; align-items: center; justify-content: center; gap: 14px;
  background: rgba(255, 255, 255, .86);
  color: var(--brand-dark); font-weight: 600;
}
.loading.open { display: flex; }
.spinner {
  width: 38px; height: 38px; border-radius: 50%;
  border: 4px solid var(--brand-light); border-top-color: var(--brand);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
#toasts { position: fixed; right: 16px; bottom: 40px; z-index: 120; display: flex; flex-direction: column; gap: 8px; }
.toast {
  max-width: 380px; padding: 10px 14px; border-radius: 6px;
  background: #323130; color: #fff; box-shadow: 0 6px 18px rgba(0, 0, 0, .25);
  opacity: 0; transform: translateY(8px); transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: none; }
.toast.ok { background: #0e6b38; }
.toast.warn { background: #8a5a00; }
.toast.error { background: #a4262c; }

/* ------------------------------------------------------------ diálogos */
.modal {
  position: fixed; inset: 0; z-index: 90;
  display: none; align-items: flex-start; justify-content: center;
  padding: 6vh 16px;
  background: rgba(0, 0, 0, .35);
  overflow: auto;
}
.modal.open { display: flex; }
.dialog {
  width: 100%; max-width: 560px;
  background: #fff; border-radius: 8px; padding: 20px 24px 24px;
  box-shadow: 0 16px 48px rgba(0, 0, 0, .3);
}
.dialog.wide { max-width: 920px; }
.dialog.narrow { max-width: 400px; }
.dlg-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.dlg-head h2 { margin: 0; font-size: 20px; color: var(--brand-dark); }
.dialog h3 { margin: 22px 0 10px; font-size: 15px; }
.x { border: 0; background: none; font-size: 26px; line-height: 1; cursor: pointer; color: var(--muted); padding: 0 4px; }
.x:hover { color: #000; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.ok { color: var(--ok); }
.warn { color: var(--warn); }
.error { color: var(--err); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 34px; padding: 0 14px;
  border: 1px solid #c8c6c4; border-radius: 4px; background: #fff; color: #201f1e;
  cursor: pointer; text-decoration: none; white-space: nowrap;
}
.btn:hover { background: #f3f2f1; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { background: #3d1a58; }
.btn.danger { color: var(--err); border-color: #e6b3b0; }
.btn.danger:hover { background: #fdf3f2; }
.btn.small { height: 28px; padding: 0 10px; font-size: 12px; }
.btn.link { border-color: transparent; color: var(--brand); }
.btn.block { width: 100%; }
.btn:disabled { opacity: .6; cursor: default; }

.import-list { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 14px; margin-top: 14px; }
.import-card { border: 1px solid var(--line); border-radius: 8px; padding: 14px 16px; display: flex; flex-direction: column; gap: 10px; }
.ic-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.ic-head strong { font-size: 16px; color: var(--brand-dark); }
.ic-actions, .row-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.ic-result:empty { display: none; }

.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 6px; }
table.list { width: 100%; border-collapse: collapse; font-size: 13px; }
table.list th { text-align: left; font-weight: 600; background: #faf9f8; padding: 8px 10px; border-bottom: 1px solid var(--line); white-space: nowrap; }
table.list td { padding: 8px 10px; border-bottom: 1px solid #f3f2f1; vertical-align: middle; }
table.list td.actions { white-space: nowrap; text-align: right; }
table.list select { height: 30px; }
.switch { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; }

.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; align-items: end; }
.form-col { display: flex; flex-direction: column; gap: 12px; }
label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: #3b3a39; }
label.btn { flex-direction: row; font-size: 14px; color: inherit; }
label.btn.primary { color: #fff; }
input[type=text], input[type=password], input:not([type]), select {
  height: 34px; padding: 0 10px;
  border: 1px solid #c8c6c4; border-radius: 4px; background: #fff;
}
input:focus, select:focus { outline: 2px solid var(--brand); outline-offset: -1px; border-color: var(--brand); }
.form-actions { display: flex; justify-content: flex-end; gap: 8px; }

/* ------------------------------------------------------------ login */
body.login-page { display: block; overflow: auto; background: var(--brand-dark); }
.login {
  min-height: 100vh;
  display: grid; grid-template-columns: 1.1fr 1fr;
}
.login-art {
  background: var(--brand);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 24px;
  padding: 40px;
}
.login-title {
  background: var(--brand-dark); color: #fff;
  font: 700 26px var(--cell-font);
  padding: 14px 32px;
  border: 3px solid #000;
  text-align: center;
}
.login-art img { width: min(520px, 100%); height: auto; }
.login-card {
  background: #fff;
  display: flex; flex-direction: column; justify-content: center;
  padding: 48px max(32px, 8vw);
}
.login-card h1 { margin: 0 0 6px; color: var(--brand-dark); font-size: 28px; }
.login-card form { display: flex; flex-direction: column; gap: 14px; margin-top: 18px; max-width: 360px; }
.login-card input { height: 40px; }
.login-card .btn { height: 42px; font-size: 15px; }
#login-error:empty { display: none; }
@media (max-width: 760px) {
  .login { grid-template-columns: 1fr; }
  .login-art { padding: 24px 16px; }
  .login-art img { max-width: 280px; }
  .login-title { font-size: 20px; }
  .login-card { padding: 32px 16px; }
  .brand span, .tb span, .who { display: none; }
  .sheet-picker select { max-width: 150px; }
  #status-text { min-width: 0; }
}

/* ------------------------------------------------------------ impressão */
@media print {
  @page { size: landscape; margin: 10mm; }
  body { display: block; overflow: visible; height: auto; }
  .topbar, .statusbar, #toasts, .loading, .modal, .dv-btn { display: none !important; }
  #viewport { overflow: visible; }
  table.grid thead th, table.grid th.rh { visibility: hidden; }
  table.grid td.sel { outline: none; }
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}
