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

:root {
  --bg: #f8f7f4;
  --bg2: #ffffff;
  --bg3: #f1efe8;
  --text: #2c2c2a;
  --text2: #5f5e5a;
  --text3: #888780;
  --border: rgba(0,0,0,0.1);
  --border2: rgba(0,0,0,0.18);
  --accent: #378ADD;
  --green: #3B6D11;
  --red: #A32D2D;
  --purple: #534AB7;
  --amber: #BA7517;
  --teal: #0F6E56;
  --radius: 8px;
  --radius-lg: 12px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1a1a18;
    --bg2: #242422;
    --bg3: #2c2c2a;
    --text: #e8e6de;
    --text2: #b4b2a9;
    --text3: #888780;
    --border: rgba(255,255,255,0.1);
    --border2: rgba(255,255,255,0.18);
  }
}

body { font-family: system-ui, -apple-system, sans-serif; background: var(--bg); color: var(--text); font-size: 15px; line-height: 1.5; }

.login-screen { min-height: 100vh; display: flex; align-items: center; justify-content: center; }
.login-card { background: var(--bg2); border: 0.5px solid var(--border); border-radius: var(--radius-lg); padding: 2rem; width: 100%; max-width: 340px; display: flex; flex-direction: column; gap: 12px; }
.login-logo { font-size: 20px; font-weight: 500; margin-bottom: 8px; }
.login-logo i { font-size: 22px; vertical-align: -3px; color: var(--accent); }
.error-msg { color: var(--red); font-size: 13px; min-height: 18px; }

.app { display: grid; grid-template-columns: 210px 1fr; min-height: 100vh; }

.sidebar { background: var(--bg3); border-right: 0.5px solid var(--border); display: flex; flex-direction: column; padding: 0; position: sticky; top: 0; height: 100vh; overflow-y: auto; }
.sidebar-logo { padding: 20px 16px 16px; font-size: 16px; font-weight: 500; border-bottom: 0.5px solid var(--border); }
.sidebar-logo i { font-size: 18px; vertical-align: -3px; color: var(--accent); margin-right: 6px; }
nav { padding: 8px 0; }
.nav-item { display: flex; align-items: center; gap: 10px; padding: 9px 16px; font-size: 14px; color: var(--text2); cursor: pointer; text-decoration: none; border-radius: 0; transition: background .15s; }
.nav-item:hover { background: var(--border); color: var(--text); }
.nav-item.active { background: var(--bg2); color: var(--text); font-weight: 500; }
.nav-item i { font-size: 17px; }
.sidebar-mesos { padding: 16px 0 8px; border-top: 0.5px solid var(--border); margin-top: 8px; flex: 1; }
.sidebar-mesos-title { padding: 0 16px 6px; font-size: 11px; color: var(--text3); text-transform: uppercase; letter-spacing: .05em; }
.sidebar-bottom { border-top: 0.5px solid var(--border); padding: 8px 0; }

.main { padding: 24px; overflow-y: auto; }
.page-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 20px; }
.page-title { font-size: 20px; font-weight: 500; }

input, select, textarea { width: 100%; padding: 8px 12px; border: 0.5px solid var(--border2); border-radius: var(--radius); background: var(--bg2); color: var(--text); font-size: 14px; font-family: inherit; }
input:focus, select:focus { outline: none; border-color: var(--accent); }
button { padding: 8px 16px; border: 0.5px solid var(--border2); border-radius: var(--radius); background: var(--bg3); color: var(--text); min-width: 32px; font-size: 14px; cursor: pointer; font-family: inherit; transition: background .15s; }
button:hover { background: var(--bg3); }
button.primary { background: var(--accent); color: #fff; border-color: var(--accent); }
button.primary:hover { opacity: .9; }
button.danger { background: var(--red); color: #fff; border-color: var(--red); }
button.small { padding: 4px 10px; font-size: 12px; }

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; margin-bottom: 20px; }
.kpi { background: var(--bg3); border-radius: var(--radius); padding: 12px 14px; }
.kpi-label { font-size: 11px; color: var(--text3); margin-bottom: 4px; }
.kpi-value { font-size: 22px; font-weight: 500; }
.kpi-sub { font-size: 11px; color: var(--text3); margin-top: 2px; }
.kpi-value.green { color: var(--green); }
.kpi-value.red { color: var(--red); }

.charts-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; margin-bottom: 16px; }
.chart-card { border: 0.5px solid var(--border); border-radius: var(--radius-lg); padding: 16px; background: var(--bg2); }
.chart-card.full { grid-column: 1 / -1; }
.chart-title { font-size: 13px; font-weight: 500; margin-bottom: 10px; }
.chart-wrap { position: relative; height: 160px; }
.chart-wrap canvas { position: absolute; inset: 0; width: 100% !important; height: 100% !important; }

.legend { display: flex; gap: 12px; margin-bottom: 8px; flex-wrap: wrap; }
.leg-item { display: flex; align-items: center; gap: 5px; font-size: 12px; color: var(--text2); }
.leg-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }

.guardioles-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 8px; }
.gcard { border: 0.5px solid var(--border); border-radius: var(--radius); padding: 10px 12px; background: var(--bg2); }
.gcard-name { font-size: 11px; color: var(--text3); margin-bottom: 3px; }
.gcard-val { font-size: 17px; font-weight: 500; }
.gcard-bar-bg { height: 3px; border-radius: 2px; margin-top: 8px; background: var(--border); }
.gcard-fill { height: 3px; border-radius: 2px; }

.mode-toggle { display: flex; border: 0.5px solid var(--border2); border-radius: var(--radius); overflow: hidden; }
.mode-btn { padding: 6px 14px; font-size: 12px; cursor: pointer; background: transparent; border: none; color: var(--text2); }
.mode-btn.active { background: var(--bg3); color: var(--text); font-weight: 500; }

.section { background: var(--bg2); border: 0.5px solid var(--border); border-radius: var(--radius-lg); padding: 16px; margin-bottom: 14px; }
.section-title { font-size: 14px; font-weight: 500; margin-bottom: 12px; display: flex; align-items: center; justify-content: space-between; }

table { width: 100%; border-collapse: collapse; font-size: 14px; }
th { text-align: left; font-size: 12px; font-weight: 500; color: var(--text3); padding: 6px 8px; border-bottom: 0.5px solid var(--border); }
td { padding: 7px 8px; border-bottom: 0.5px solid var(--border); }
tr:last-child td { border-bottom: none; }
tr:hover td { background: var(--bg3); }

.badge { display: inline-block; padding: 2px 8px; border-radius: 20px; font-size: 11px; font-weight: 500; }
.badge.fixa { background: #EEEDFE; color: #3C3489; }
.badge.restaurant { background: #E1F5EE; color: #085041; }
.badge.altre { background: #FAEEDA; color: #633806; }
.badge.recuperat { background: #EAF3DE; color: #27500A; }

.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 10px; }
.form-group { display: flex; flex-direction: column; gap: 4px; }
.form-group label { font-size: 12px; color: var(--text2); }
.form-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 12px; }

.alert { padding: 10px 14px; border-radius: var(--radius); font-size: 13px; margin-bottom: 12px; }
.alert.success { background: #EAF3DE; color: #27500A; }
.alert.error { background: #FCEBEB; color: #791F1F; }

/* ── Bottom nav (mobile only) ── */
.bottom-nav {
  display: none;
  position: fixed;
  bottom: 0; left: 0; right: 0;
  height: 64px;
  background: var(--bg2);
  border-top: 0.5px solid var(--border);
  z-index: 50;
  align-items: center;
  justify-content: space-around;
  padding: 0 4px;
  padding-bottom: env(safe-area-inset-bottom);
}

.bottom-nav-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 6px 12px;
  color: var(--text3);
  cursor: pointer;
  text-decoration: none;
  font-size: 10px;
  flex: 1;
  transition: color .15s;
  -webkit-tap-highlight-color: transparent;
}
.bottom-nav-item i { font-size: 22px; }
.bottom-nav-item.active { color: var(--accent); }
.bottom-nav-item.active i { color: var(--accent); }

.bottom-nav-fab {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 28px;
  cursor: pointer;
  box-shadow: 0 4px 16px rgba(55, 138, 221, 0.45);
  flex-shrink: 0;
  margin-bottom: 18px;
  padding: 0;
  transition: transform .15s, opacity .15s;
  -webkit-tap-highlight-color: transparent;
}
.bottom-nav-fab:hover, .bottom-nav-fab:active { opacity: .9; transform: scale(1.07); }

/* ── Touch-friendly buttons ── */
.btn-touch { min-width: 36px; min-height: 36px; display: inline-flex; align-items: center; justify-content: center; }

/* ── Privacy eye button ── */
.btn-privacy { background: transparent; border: none; color: var(--text3); padding: 4px 6px; font-size: 18px; cursor: pointer; display: inline-flex; align-items: center; border-radius: var(--radius); transition: color .15s; }
.btn-privacy:hover { color: var(--text); background: transparent; }

/* ── Mobile subtext ── */
.show-mobile { display: none; }
.row-sub { font-size: 11px; color: var(--text3); margin-top: 1px; }

/* ── Guardioles form ── */
.guardioles-form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px,1fr)); gap: 8px; }

/* ── Historial ── */
.hist-any-selector {
  position: relative;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border: 0.5px solid var(--border2);
  border-radius: var(--radius);
  background: var(--bg2);
  cursor: pointer;
  font-size: 13px;
  color: var(--text);
  user-select: none;
}
.hist-any-selector:hover { border-color: var(--accent); }

.hist-cards { display: flex; flex-direction: column; gap: 8px; }
.hist-card {
  background: var(--bg2);
  border: 0.5px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 14px;
  cursor: pointer;
  transition: border-color .15s;
  -webkit-tap-highlight-color: transparent;
}
.hist-card:hover { border-color: var(--border2); }
.hist-card-total { background: var(--bg3); }
.hist-card-top { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.hist-card-mes { font-size: 15px; font-weight: 500; }
.hist-card-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.hist-card-kpi {}
.hist-card-label { font-size: 10px; color: var(--text3); margin-bottom: 2px; }
.hist-card-val { font-size: 14px; font-weight: 500; }
.hist-card-val.green { color: var(--green); }
.hist-card-val.red { color: var(--red); }

.show-mobile-block { display: none; }

/* ── Mes selector dropdown ── */
.mes-selector {
  position: relative;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border: 0.5px solid var(--border2);
  border-radius: var(--radius);
  background: var(--bg2);
  cursor: pointer;
  font-size: 13px;
  color: var(--text);
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  transition: border-color .15s;
}
.mes-selector:hover { border-color: var(--accent); }
.mes-selector.open { border-color: var(--accent); }
.mes-selector-chevron { font-size: 14px; color: var(--text3); transition: transform .2s; flex-shrink: 0; }
.mes-selector.open .mes-selector-chevron { transform: rotate(180deg); }

.mes-selector-dropdown {
  display: none;
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  left: auto;
  min-width: 160px;
  background: var(--bg2);
  border: 0.5px solid var(--border2);
  border-radius: var(--radius-lg);
  box-shadow: 0 8px 24px rgba(0,0,0,0.12);
  z-index: 100;
  max-height: 260px;
  overflow-y: auto;
  padding: 4px;
}
.mes-selector-dropdown.open { display: block; }

.mes-selector-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  font-size: 13px;
  border-radius: 6px;
  cursor: pointer;
  color: var(--text2);
  transition: background .1s;
}
.mes-selector-item:hover { background: var(--bg3); color: var(--text); }
.mes-selector-item.active { color: var(--accent); font-weight: 500; }

/* ── Mes Hero ── */
.mes-hero { text-align: center; padding: 20px 16px 16px; margin-bottom: 10px; }
.mes-hero-label { font-size: 11px; color: var(--text3); text-transform: uppercase; letter-spacing: .06em; margin-bottom: 4px; }
.mes-hero-value { font-size: 42px; font-weight: 600; line-height: 1.1; }
.mes-hero-sub { display: flex; justify-content: center; gap: 12px; font-size: 13px; color: var(--text2); margin-top: 8px; flex-wrap: wrap; }

/* ── Mes mini cards ── */
.mes-cards { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-bottom: 14px; }
.mes-card { background: var(--bg3); border-radius: var(--radius); padding: 10px 12px; }
.mes-card-label { font-size: 11px; color: var(--text3); margin-bottom: 2px; }
.mes-card-value { font-size: 16px; font-weight: 500; }
.mes-card-sub { font-size: 10px; color: var(--text3); margin-top: 2px; }
.mes-card-value.green { color: var(--green); }
.mes-card-value.red { color: var(--red); }

/* Perill dropdown: align left not right */
#perill-dropdown { right: auto; left: 0; }
/* Prevent section from clipping dropdowns */
.section { overflow: visible; }

/* ── Mes accordion ── */
.mes-accordion { margin-bottom: 8px; }
.mes-accordion > summary { list-style: none; cursor: pointer; -webkit-tap-highlight-color: transparent; }
.mes-accordion > summary::-webkit-details-marker { display: none; }
.mes-accordion-header { display: flex; justify-content: space-between; align-items: center; }
.mes-accordion[open] .mes-accordion-header { margin-bottom: 12px; }
.mes-accordion-left { display: flex; align-items: center; gap: 10px; }
.mes-accordion-label { font-size: 14px; font-weight: 500; }
.mes-accordion-right { display: flex; align-items: center; gap: 8px; }
.mes-accordion-total { font-size: 14px; color: var(--text2); }
.mes-accordion-chevron { font-size: 16px; color: var(--text3); transition: transform .2s; flex-shrink: 0; }
.mes-accordion:not([open]) .mes-accordion-chevron { transform: rotate(-90deg); }
.mes-accordion-icon { width: 34px; height: 34px; border-radius: 8px; display: flex; align-items: center; justify-content: center; font-size: 16px; flex-shrink: 0; }
.mes-accordion-body { padding-top: 4px; }

/* ── Mobile ── */
@media (max-width: 768px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { display: none; }
  .bottom-nav { display: flex; }
  .main { padding: 12px; padding-bottom: 84px; }
  .charts-grid { grid-template-columns: 1fr; }
  .form-row { grid-template-columns: 1fr; }
  .kpis { grid-template-columns: repeat(2, 1fr); }
  .page-header { flex-wrap: wrap; gap: 8px; }
  .hide-mobile { display: none !important; }

  /* Hero */
  .mes-hero-value { font-size: 36px; }

  /* Historial cards */
  .show-mobile-block { display: flex; flex-direction: column; }
  .hist-card-row { grid-template-columns: repeat(2, 1fr); }
  .hist-card-total .hist-card-row { grid-template-columns: repeat(3, 1fr); }

  /* Cards: 2x2 on mobile */
  .mes-cards { grid-template-columns: repeat(2, 1fr); }

  /* Hide date columns, show as subtext */
  .col-dia, .col-data { display: none; }
  .show-mobile { display: block; }

  /* Bigger touch targets */
  td { padding: 10px 6px; }
  .btn-touch { min-width: 40px; min-height: 40px; }

  /* Guardioles 2x2 */
  .guardioles-form-grid { grid-template-columns: 1fr 1fr; }
}
