@layer reset, base, layout, components, utilities;

@layer reset {
  *, *::before, *::after { box-sizing: border-box; }
  * { margin: 0; }
  html { scroll-behavior: smooth; }
  body { min-height: 100dvh; }
  button, input { font: inherit; }
  button { border: 0; }
  img, svg { display: block; }
  a { color: inherit; text-decoration: none; }
}

@layer base {
  :root {
    --ink: #171a18;
    --muted: #7d827d;
    --paper: #f3f5f1;
    --surface: #ffffff;
    --line: #e5e8e2;
    --lime: #c6f257;
    --lime-dark: #a9dc2b;
    --green: #1f7a59;
    --red: #e05d59;
    --blue: #5b7ae8;
    --orange: #ef9c4c;
    --radius: 20px;
    --shadow: 0 18px 50px rgba(38, 48, 40, .07);
  }

  body {
    color: var(--ink);
    background: var(--paper);
    font: 14px/1.45 Inter, 'Segoe UI', system-ui, sans-serif;
    -webkit-font-smoothing: antialiased;
  }

  h1, h2, strong, b { font-family: Inter, 'Segoe UI', system-ui, sans-serif; }
  button, a { -webkit-tap-highlight-color: transparent; }
  button:focus-visible, a:focus-visible, summary:focus-visible { outline: 3px solid rgba(115, 155, 22, .38); outline-offset: 3px; }
  ::selection { background: var(--lime); color: var(--ink); }
}

@layer layout {
  .app-shell { min-height: 100dvh; }
  .sidebar { position: fixed; inset: 0 auto 0 0; z-index: 20; display: flex; width: 238px; flex-direction: column; padding: 30px 22px 22px; color: #f8faf6; background: #171a18; }
  .main { width: auto; max-width: 1540px; margin-left: 238px; padding: 0 40px 42px; }
  .dashboard-grid { display: grid; grid-template-columns: minmax(0, 2fr) minmax(270px, .75fr); gap: 16px; margin-top: 16px; }
  .dashboard-grid--lower { grid-template-columns: minmax(0, 1.55fr) minmax(300px, .85fr); }
}

@layer components {
  .brand { display: flex; align-items: center; gap: 12px; padding: 0 5px; }
  .brand__mark { display: grid; width: 38px; height: 38px; place-items: center; color: var(--ink); background: var(--lime); border-radius: 10px 10px 4px 10px; font: 800 23px/1 Inter, 'Segoe UI', sans-serif; }
  .brand b { display: block; font-size: 15px; letter-spacing: .08em; }
  .brand small { display: block; margin-top: 1px; color: #878e87; font-size: 9px; letter-spacing: .22em; }

  .nav { display: grid; gap: 6px; margin-top: 58px; }
  .nav__item { display: flex; align-items: center; gap: 13px; min-height: 46px; padding: 0 13px; color: #949a94; border-radius: 12px; font-weight: 600; transition: .18s ease; }
  .nav__item svg { width: 19px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
  .nav__item:first-child svg { fill: currentColor; stroke: none; }
  .nav__item:hover { color: white; background: #222723; }
  .nav__item--active { color: var(--ink); background: var(--lime); }
  .nav__item--active:hover { color: var(--ink); background: var(--lime); }

  .sidebar__device { margin-top: auto; padding: 17px; background: #212522; border: 1px solid #2d332e; border-radius: 16px; }
  .sidebar .eyebrow { color: #737a74; }
  .watch-mini { display: flex; align-items: center; gap: 12px; margin: 13px 0; }
  .watch-mini__icon { position: relative; display: grid; width: 30px; height: 40px; place-items: center; background: #0e100f; border: 2px solid #59605a; border-radius: 9px; }
  .watch-mini__icon::before, .watch-mini__icon::after { position: absolute; left: 9px; width: 8px; height: 7px; content: ''; background: #59605a; }
  .watch-mini__icon::before { top: -8px; border-radius: 3px 3px 0 0; } .watch-mini__icon::after { bottom: -8px; border-radius: 0 0 3px 3px; }
  .watch-mini__icon span { width: 5px; height: 5px; background: var(--lime); border-radius: 50%; box-shadow: 0 0 9px var(--lime); }
  .watch-mini b, .watch-mini small { display: block; overflow: hidden; max-width: 112px; text-overflow: ellipsis; white-space: nowrap; }
  .watch-mini b { font-size: 12px; } .watch-mini small, .sidebar__device > small { color: #868d87; font-size: 10px; }
  .battery-line { overflow: hidden; height: 4px; margin: 15px 0 8px; background: #373d38; border-radius: 8px; }
  .battery-line span { display: block; width: 0; height: 100%; background: var(--lime); transition: width .5s ease; }
  .sidebar__help { display: flex; gap: 10px; align-items: center; margin-top: 14px; padding: 10px 4px; color: #9ba09b; text-align: left; background: transparent; cursor: pointer; }
  .sidebar__help > span:first-child { display: grid; width: 28px; height: 28px; place-items: center; border: 1px solid #424843; border-radius: 50%; }
  .sidebar__help b, .sidebar__help small { display: block; } .sidebar__help b { color: #dfe3df; font-size: 11px; } .sidebar__help small { font-size: 9px; }

  .topbar { display: flex; min-height: 124px; align-items: center; justify-content: space-between; gap: 24px; border-bottom: 1px solid var(--line); }
  .topbar__title h1 { margin-top: 2px; font-size: clamp(27px, 3vw, 39px); line-height: 1.1; letter-spacing: -.045em; }
  .accent-dot { color: var(--lime-dark); }
  .eyebrow { color: #929791; font: 700 10px/1.2 Inter, 'Segoe UI', sans-serif; letter-spacing: .16em; }
  .topbar__actions { display: flex; align-items: center; gap: 12px; }
  .user-pill { display: flex; align-items: center; gap: 9px; padding: 5px 8px 5px 5px; color: var(--ink); text-align: left; background: transparent; border-radius: 11px; cursor: pointer; }
  .user-pill:hover { background: #e9ece6; }
  .user-pill > span:first-child { display: grid; width: 32px; height: 32px; place-items: center; color: #4f6813; background: var(--lime); border-radius: 9px; font-weight: 800; }
  .user-pill b, .user-pill small { display: block; max-width: 145px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .user-pill b { font-size: 10px; } .user-pill small { color: var(--muted); font-size: 8px; }
  .user-pill.cloud-ok small { color: var(--green); }
  .user-pill.cloud-error small { color: var(--red); }
  .connection-pill { display: flex; align-items: center; gap: 8px; color: #858a85; font-size: 12px; }
  .connection-pill i, .live-indicator i { width: 8px; height: 8px; background: #a8ada8; border-radius: 50%; }
  .connection-pill.connected i, .live-indicator.active i { background: #53b77c; box-shadow: 0 0 0 5px rgba(83, 183, 124, .13); }
  .icon-button.menu-button { display: none; }

  .button { display: inline-flex; min-height: 43px; align-items: center; justify-content: center; gap: 9px; padding: 0 17px; color: var(--ink); background: white; border-radius: 11px; font-weight: 700; cursor: pointer; transition: transform .16s ease, opacity .16s ease, background .16s ease; }
  .button:hover:not(:disabled) { transform: translateY(-1px); }
  .button:active:not(:disabled) { transform: translateY(0); }
  .button:disabled { opacity: .42; cursor: not-allowed; }
  .button svg { width: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
  .button--dark { color: white; background: var(--ink); }
  .button--dark:hover:not(:disabled) { background: #2d322e; }
  .button--lime { background: var(--lime); box-shadow: 0 8px 25px rgba(174, 219, 59, .2); }
  .button--lime:hover:not(:disabled) { background: #bceb45; }
  .button--outline { min-height: 37px; color: #575c57; background: white; border: 1px solid var(--line); }
  .icon-button { display: grid; width: 42px; height: 42px; place-items: center; background: white; border-radius: 11px; }
  .icon-button svg { width: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; }

  .intro-row { display: flex; align-items: flex-end; justify-content: space-between; gap: 20px; padding: 31px 0 21px; }
  .intro-row p { font: 700 17px Inter, 'Segoe UI', sans-serif; }
  .intro-row div span { color: var(--muted); font-size: 12px; }

  .metric-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 16px; }
  .metric-card { position: relative; overflow: hidden; min-height: 193px; padding: 19px 20px 16px; background: var(--surface); border: 1px solid rgba(222, 226, 219, .75); border-radius: var(--radius); box-shadow: 0 8px 30px rgba(45, 55, 47, .035); }
  .metric-card__top { display: flex; align-items: center; justify-content: space-between; }
  .metric-icon { display: grid; min-width: 34px; height: 34px; padding: 0 6px; place-items: center; color: var(--green); background: #eef6f1; border-radius: 10px; font: 700 13px Inter, 'Segoe UI', sans-serif; }
  .metric-card--heart .metric-icon { color: var(--red); background: #fff0ef; }
  .metric-card--spo2 .metric-icon { color: var(--blue); background: #eef1ff; }
  .metric-card--steps .metric-icon { color: #668a10; background: #f0f8da; }
  .metric-card--temperature .metric-icon { color: var(--orange); background: #fff3e7; font-size: 20px; }
  .metric-state { color: #8b908b; font-size: 11px; }
  .metric-card__value { display: flex; align-items: baseline; gap: 7px; margin-top: 14px; }
  .metric-card__value strong { font-size: clamp(27px, 2.5vw, 35px); line-height: 1; letter-spacing: -.05em; }
  .metric-card__value span { color: #8c918c; font-size: 10px; }
  .metric-card > small { position: absolute; bottom: 15px; left: 20px; color: #949994; font-size: 9px; }
  .spark { position: absolute; right: 17px; bottom: 34px; width: 46%; height: 40px; opacity: .78; }
  .spark path { fill: none; stroke: currentColor; stroke-width: 2; vector-effect: non-scaling-stroke; }
  .spark path.area { stroke: none; opacity: .09; }
  .metric-card--heart .spark { color: var(--red); } .metric-card--spo2 .spark { color: var(--blue); } .metric-card--steps .spark { color: #91bb26; } .metric-card--temperature .spark { color: var(--orange); }

  .panel { background: var(--surface); border: 1px solid rgba(222, 226, 219, .8); border-radius: var(--radius); box-shadow: var(--shadow); }
  .activity-panel, .goal-panel, .sleep-panel, .vitals-panel { min-height: 325px; padding: 24px 25px; }
  .panel__header { display: flex; align-items: flex-start; justify-content: space-between; gap: 18px; }
  .panel__header h2 { margin-top: 4px; font-size: 20px; letter-spacing: -.035em; }
  .panel__header p { margin-top: 5px; color: var(--muted); font-size: 11px; }
  .segmented { display: flex; padding: 3px; background: #f1f3ef; border-radius: 9px; }
  .segmented button { padding: 6px 10px; color: #969b96; background: transparent; border-radius: 7px; font-size: 10px; cursor: pointer; }
  .segmented button.active { color: var(--ink); background: white; box-shadow: 0 2px 8px rgba(32, 42, 34, .08); font-weight: 700; }
  .chart-summary { display: flex; gap: 45px; margin-top: 24px; }
  .chart-summary strong, .chart-summary span { display: block; } .chart-summary strong { font-size: 19px; } .chart-summary span { color: #969b96; font-size: 9px; }
  .bar-chart { display: flex; height: 150px; align-items: flex-end; gap: 10px; margin-top: 10px; padding-top: 12px; border-bottom: 1px solid var(--line); background-image: linear-gradient(to bottom, transparent calc(33% - 1px), #eef0ec 33%, transparent calc(33% + 1px), transparent calc(66% - 1px), #eef0ec 66%, transparent calc(66% + 1px)); }
  .bar-chart__item { display: flex; flex: 1; height: 100%; align-items: center; justify-content: flex-end; flex-direction: column; gap: 7px; }
  .bar-chart__bar { width: min(24px, 65%); min-height: 3px; background: #dce5c8; border-radius: 7px 7px 2px 2px; transition: height .45s ease; }
  .bar-chart__item:last-child .bar-chart__bar { background: var(--lime); }
  .bar-chart__item span { height: 14px; color: #a1a6a1; font-size: 8px; text-transform: uppercase; }

  .goal-panel { display: flex; flex-direction: column; }
  .goal-ring { display: grid; width: 152px; height: 152px; margin: 17px auto 13px; place-items: center; background: conic-gradient(var(--lime) var(--goal, 0%), #eef0eb 0); border-radius: 50%; transition: background .5s ease; }
  .goal-ring::before { position: absolute; width: 118px; height: 118px; content: ''; background: white; border-radius: 50%; }
  .goal-ring > div { z-index: 1; text-align: center; }
  .goal-ring strong, .goal-ring span { display: block; } .goal-ring strong { font-size: 26px; letter-spacing: -.05em; } .goal-ring span { color: var(--muted); font-size: 9px; }
  .goal-stats { display: flex; justify-content: center; gap: 22px; margin-top: auto; color: var(--muted); font-size: 9px; }
  .goal-stats b { color: var(--ink); font-size: 10px; }
  .dot { display: inline-block; width: 6px; height: 6px; margin-right: 4px; background: #dfe3dc; border-radius: 50%; } .dot--lime { background: var(--lime-dark); }

  .sleep-main { margin-top: 27px; } .sleep-main strong { display: block; font-size: 31px; letter-spacing: -.05em; } .sleep-main span { color: var(--muted); font-size: 9px; }
  .quality-badge { padding: 6px 9px; color: #737973; background: #f2f4f0; border-radius: 8px; font-size: 9px; }
  .sleep-timeline { display: flex; overflow: hidden; height: 34px; align-items: flex-end; gap: 1px; margin: 28px 0 15px; border-radius: 5px; }
  .sleep-timeline span { min-width: 2px; flex: 1; background: #e8ebe6; border-radius: 2px 2px 0 0; }
  .sleep-timeline .stage-0 { height: 28%; background: #d9ddd8; } .sleep-timeline .stage-1 { height: 62%; background: #a9bee8; } .sleep-timeline .stage-2 { height: 100%; background: #536fba; } .sleep-timeline .stage-3 { height: 45%; background: #b5c9ee; }
  .sleep-legend { display: flex; flex-wrap: wrap; gap: 18px; color: #858a85; font-size: 9px; }
  .sleep-dot { display: inline-block; width: 7px; height: 7px; margin-right: 5px; background: #d9ddd8; border-radius: 50%; } .sleep-dot--deep { background: #536fba; } .sleep-dot--light { background: #a9bee8; }
  .vital-row { display: flex; align-items: baseline; justify-content: space-between; padding: 17px 0; border-bottom: 1px solid #eef0ed; color: var(--muted); font-size: 11px; }
  .vital-row:last-child { border: 0; } .vital-row strong { color: var(--ink); font-size: 17px; } .vital-row small { color: var(--muted); font: 400 9px Inter, 'Segoe UI', sans-serif; }

  .live-panel, .history-panel, .sensors-panel { margin-top: 16px; padding: 25px; scroll-margin-top: 18px; }
  .live-indicator { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 10px; }
  .live-actions { display: grid; grid-template-columns: repeat(4, minmax(150px, 1fr)) auto; gap: 10px; margin-top: 22px; }
  .action-tile { display: flex; align-items: center; gap: 11px; min-height: 64px; padding: 10px 12px; color: var(--ink); text-align: left; background: #f7f8f5; border: 1px solid #ecefe9; border-radius: 13px; cursor: pointer; transition: .15s ease; }
  .action-tile:hover:not(:disabled), .action-tile.active { background: #eff7dc; border-color: #d5ea9c; }
  .action-tile:disabled { opacity: .45; cursor: not-allowed; }
  .action-tile__icon { display: grid; min-width: 34px; height: 34px; place-items: center; color: #638215; background: white; border-radius: 9px; font: 700 13px Inter, 'Segoe UI', sans-serif; }
  .action-tile b, .action-tile small { display: block; } .action-tile b { font-size: 11px; } .action-tile small { color: var(--muted); font-size: 8px; }
  .ecg-chart { width: 100%; height: 0; margin-top: 0; color: var(--red); background-image: linear-gradient(#f1e9e8 1px, transparent 1px), linear-gradient(90deg, #f1e9e8 1px, transparent 1px); background-size: 15px 15px; border-radius: 10px; opacity: 0; transition: height .25s, margin .25s, opacity .25s; }
  .ecg-chart.visible { height: 150px; margin-top: 20px; opacity: 1; }
  .ecg-chart path { fill: none; stroke: currentColor; stroke-width: 1.5; vector-effect: non-scaling-stroke; }

  .sensor-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(205px, 1fr)); gap: 10px; margin-top: 22px; }
  .sensor-card { display: flex; min-height: 72px; align-items: center; gap: 11px; padding: 13px; background: #f7f8f5; border: 1px solid #ecefe9; border-radius: 13px; }
  .sensor-card__icon { display: grid; min-width: 36px; height: 36px; place-items: center; color: #777d77; background: white; border-radius: 10px; font-weight: 800; }
  .sensor-card b, .sensor-card small { display: block; } .sensor-card b { font-size: 10px; } .sensor-card small { margin-top: 2px; color: var(--muted); font-size: 8px; }
  .sensor-card__status { margin-left: auto; width: 9px; height: 9px; background: #c8cdc7; border-radius: 50%; }
  .sensor-card[data-status="supported"] .sensor-card__status { background: #4bb276; box-shadow: 0 0 0 5px rgba(75,178,118,.11); }
  .sensor-card[data-status="no_data"] .sensor-card__status { background: #e1a246; }
  .sensor-card[data-status="no_response"], .sensor-card[data-status="error"] { opacity: .68; }
  .sensor-card[data-status="error"] .sensor-card__status { background: var(--red); }
  .sensor-progress { position: relative; overflow: hidden; height: 34px; margin-top: 15px; color: #697069; background: #f0f2ee; border-radius: 9px; }
  .sensor-progress span { position: absolute; inset: 0 auto 0 0; width: var(--progress, 0%); background: #e1f2b5; transition: width .3s ease; }
  .sensor-progress b { position: relative; z-index: 1; display: grid; height: 100%; place-items: center; font-size: 9px; }

  .export-actions { display: flex; gap: 7px; }
  .history-tabs { display: flex; overflow-x: auto; gap: 5px; margin: 23px 0 12px; padding-bottom: 5px; scrollbar-width: thin; }
  .history-tabs button { flex: 0 0 auto; padding: 7px 11px; color: #848984; background: #f3f5f1; border-radius: 8px; font-size: 9px; cursor: pointer; }
  .history-tabs button.active { color: var(--ink); background: var(--lime); font-weight: 700; }
  .table-wrap { overflow-x: auto; min-height: 170px; border: 1px solid var(--line); border-radius: 12px; }
  table { width: 100%; border-collapse: collapse; font-size: 10px; }
  th { padding: 11px 14px; color: #929792; text-align: left; background: #f7f8f5; font-size: 8px; letter-spacing: .08em; text-transform: uppercase; white-space: nowrap; }
  td { padding: 11px 14px; border-top: 1px solid #eef0ed; white-space: nowrap; }
  tbody tr:hover { background: #fbfcfa; }
  .empty-row { height: 125px; color: var(--muted); text-align: center; }

  .protocol-panel { margin-top: 16px; padding: 0 25px; scroll-margin-top: 18px; }
  .protocol-panel summary { display: flex; min-height: 75px; align-items: center; justify-content: space-between; cursor: pointer; list-style: none; }
  .protocol-panel summary::-webkit-details-marker { display: none; }
  .protocol-panel summary b, .protocol-panel summary small { display: block; } .protocol-panel summary b { font-size: 13px; } .protocol-panel summary small, .protocol-panel summary > span:last-child { color: var(--muted); font-size: 9px; }
  .log-toolbar { display: flex; justify-content: space-between; padding: 9px 0; border-top: 1px solid var(--line); color: var(--muted); font-size: 9px; }
  .log-toolbar button { color: var(--muted); background: transparent; cursor: pointer; }
  .protocol-log { overflow: auto; max-height: 320px; margin-bottom: 22px; padding: 12px; color: #c7d2c7; background: #171a18; border-radius: 11px; font: 10px/1.7 ui-monospace, SFMono-Regular, Consolas, monospace; }
  .protocol-log p { display: grid; grid-template-columns: 56px 24px 1fr; gap: 8px; }
  .protocol-log .tx { color: #d9ee9c; } .protocol-log .rx { color: #9fc0ef; }

  footer { padding: 30px 0 0; color: #9ba09b; text-align: center; font-size: 9px; } footer span { margin: 0 6px; }

  .toast-region { position: fixed; right: 22px; bottom: 22px; z-index: 60; display: grid; gap: 9px; }
  .toast { max-width: 360px; padding: 13px 16px; color: white; background: #242824; border-radius: 12px; box-shadow: 0 15px 50px rgba(0, 0, 0, .2); font-size: 11px; animation: toast-in .22s ease; }
  .toast--error { background: #9e403e; }
  @keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

  .modal[hidden] { display: none; }
  .modal { position: fixed; inset: 0; z-index: 80; display: grid; place-items: center; padding: 20px; }
  .modal__backdrop { position: absolute; inset: 0; background: rgba(15, 18, 16, .62); backdrop-filter: blur(4px); }
  .modal__card { position: relative; width: min(470px, 100%); padding: 31px; background: white; border-radius: 21px; box-shadow: 0 30px 100px rgba(0,0,0,.25); }
  .modal__card h2 { margin: 7px 0 16px; font-size: 24px; }
  .modal__card ol { display: grid; gap: 9px; padding-left: 20px; color: #565c56; }
  .modal__card p { margin: 19px 0; padding: 12px; color: #686e68; background: #f3f5f1; border-radius: 10px; font-size: 11px; }
  .field { display: grid; gap: 7px; margin: 0 0 18px; color: #727872; font-size: 10px; }
  .field input { width: 100%; height: 44px; padding: 0 12px; color: var(--ink); background: #f7f8f5; border: 1px solid var(--line); border-radius: 10px; outline: none; }
  .field input:focus { border-color: #a8d53d; box-shadow: 0 0 0 3px rgba(168,213,61,.16); }
  .modal__close { position: absolute; top: 15px; right: 17px; width: 32px; height: 32px; color: #888e88; background: #f2f4f0; border-radius: 50%; font-size: 20px; cursor: pointer; }
}

@layer utilities {
  @media (max-width: 1120px) {
    .metric-grid { grid-template-columns: repeat(2, 1fr); }
    .live-actions { grid-template-columns: repeat(2, 1fr); }
    #stop-live { grid-column: 1 / -1; }
  }

  @media (max-width: 820px) {
    .app-shell { display: block; }
    .sidebar { transform: translateX(-100%); transition: transform .22s ease; }
    body.menu-open .sidebar { transform: translateX(0); box-shadow: 15px 0 60px rgba(0,0,0,.25); }
    .main { margin-left: 0; padding: 0 20px 35px; }
    .icon-button.menu-button { display: grid; }
    .topbar { min-height: 100px; }
    .topbar__title { margin-right: auto; }
    .topbar__title .eyebrow, .connection-pill, .user-pill > span:last-child { display: none; }
    .topbar__actions .button span { display: none; }
    .topbar__actions .button { width: 44px; padding: 0; }
    .dashboard-grid, .dashboard-grid--lower { grid-template-columns: 1fr; }
  }

  @media (max-width: 560px) {
    .main { padding-inline: 13px; }
    .topbar { gap: 8px; }
    .topbar__title h1 { font-size: 25px; }
    .intro-row { align-items: stretch; flex-direction: column; }
    .intro-row .button { width: 100%; }
    .metric-grid { grid-template-columns: 1fr; gap: 10px; }
    .metric-card { min-height: 158px; }
    .activity-panel, .goal-panel, .sleep-panel, .vitals-panel, .live-panel, .history-panel { padding: 19px; }
    .chart-summary { gap: 22px; }
    .panel__header { flex-direction: column; }
    .goal-panel .panel__header { flex-direction: row; }
    .live-actions { grid-template-columns: 1fr; }
    .sleep-legend { gap: 10px; }
    .export-actions { width: 100%; }
    .export-actions .button { flex: 1; }
  }

  @media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; animation: none !important; }
  }
}
