:root {
  --navy: #0F2133;
  --navy-2: #1B3550;
  --teal: #12A5A0;
  --teal-dark: #0B7F7B;
  --teal-soft: #E6F5F4;
  --bg: #F7F8FA;
  --surface: #FFFFFF;
  --text: #14202C;
  --muted: #66727E;
  --line: #E6E9EE;
  --warning: #D9922B;
  --error: #B94A48;
  --radius: 16px;
  --topbar-h: 52px;
  --nav-h: 62px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { scroll-behavior: smooth; scroll-padding-top: 72px; scroll-padding-bottom: 96px; }
body {
  margin: 0;
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.45;
  color: var(--text);
  background: var(--bg);
  overscroll-behavior-y: none;
  -webkit-text-size-adjust: 100%;
}
body.lock { overflow: hidden; }
h1, h2, h3, p { margin: 0; }
button { font: inherit; color: inherit; }
:focus-visible { outline: 2.5px solid var(--teal); outline-offset: 2px; }

/* Top bar */
.topbar {
  position: fixed; top: 0; left: 0; right: 0; z-index: 20;
  padding-top: var(--safe-top);
  height: calc(var(--topbar-h) + var(--safe-top));
  background: var(--bg);
  display: flex; align-items: center; justify-content: space-between;
  padding-left: max(16px, env(safe-area-inset-left));
  padding-right: max(16px, env(safe-area-inset-right));
}
.topbar.compact-home { height: calc(16px + var(--safe-top)); }
.topbar-left { display: flex; align-items: center; gap: 6px; }
.topbar h1 { font-size: 20px; font-weight: 700; color: var(--navy); letter-spacing: -.2px; }
.icon-btn {
  width: 40px; height: 40px; margin-left: -8px; border: 0; border-radius: 50%;
  background: transparent; color: var(--navy); display: grid; place-items: center; cursor: pointer;
}
.icon-btn[hidden] { display: none; }
.icon-btn:active { background: var(--line); }
.offline-badge {
  font-size: 12px; font-weight: 600; padding: 3px 10px; border-radius: 99px;
  background: #FBEFD9; color: #8A5A10;
}
.offline-badge[hidden] { display: none; }

/* Main container */
#app {
  max-width: 560px; margin: 0 auto;
  padding: calc(var(--topbar-h) + var(--safe-top) + 4px) 16px calc(var(--nav-h) + var(--safe-bottom) + 28px);
  padding-left: max(16px, env(safe-area-inset-left));
  padding-right: max(16px, env(safe-area-inset-right));
}
.screen { animation: fade .14s ease-out; }
.screen[hidden] { display: none; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
@media (prefers-reduced-motion: reduce) { .screen { animation: none; } * { scroll-behavior: auto !important; } }

/* Home */
.home-head { display: flex; align-items: center; gap: 12px; margin: 4px 2px 24px; }
.home-head img { border-radius: 10px; }
.home-brand { font-size: 22px; font-weight: 800; letter-spacing: 1.2px; color: var(--navy); line-height: 1.1; }
.home-tag { font-size: 14px; color: var(--muted); }

.tool-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; }
.tile {
  display: flex; flex-direction: column; justify-content: space-between; gap: 18px;
  min-height: 112px; padding: 16px; text-decoration: none; color: var(--text);
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  transition: transform .08s, background .12s;
}
.tile:active { transform: scale(.98); background: var(--teal-soft); }
.tile-text { display: flex; flex-direction: column; gap: 2px; }
.tile-text strong { font-size: 15.5px; font-weight: 650; color: var(--navy); }
.tile-text span { font-size: 13px; color: var(--muted); }

.icon-wrap {
  display: grid; place-items: center; flex: none;
  width: 40px; height: 40px; border-radius: 11px;
  background: var(--teal-soft); color: var(--teal-dark);
}
.icon-wrap.lg { width: 52px; height: 52px; border-radius: 14px; }
.ico { width: 22px; height: 22px; flex: none; }

.quiet-note {
  margin-top: 20px; font-size: 13px; color: var(--muted); text-align: center; line-height: 1.5;
}
.quiet-note:empty { display: none; }

/* Tab switchers (Ukur & Kalkulator) */
.tab-mount { margin-top: 16px; }
.segmented {
  display: flex; background: #EAECEF; border-radius: 12px; padding: 3px; gap: 3px;
}
.segmented button {
  flex: 1; min-height: 40px; border: 0; border-radius: 9px; background: transparent;
  font-size: 14.5px; font-weight: 600; color: var(--muted); cursor: pointer;
}
.segmented button[aria-selected="true"],
.segmented button[aria-pressed="true"] {
  background: var(--surface); color: var(--navy); box-shadow: 0 1px 2px rgba(15,33,51,.1);
}
.segmented.small button { min-height: 36px; font-size: 13.5px; }

.pill-bar {
  display: flex; gap: 8px; overflow-x: auto; padding-bottom: 2px;
  scrollbar-width: none; -webkit-overflow-scrolling: touch;
}
.pill-bar::-webkit-scrollbar { display: none; }
.pill-bar button {
  flex: none; min-height: 38px; padding: 0 15px; border: 1px solid var(--line); border-radius: 99px;
  background: var(--surface); font-size: 14px; font-weight: 600; color: var(--muted); cursor: pointer;
}
.pill-bar button[aria-selected="true"] {
  background: var(--navy); border-color: var(--navy); color: #fff;
}

/* List (Settings) */
.list {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden;
}
.row {
  display: flex; align-items: center; gap: 12px; width: 100%;
  min-height: 58px; padding: 12px 16px; text-align: left; text-decoration: none; color: inherit;
  background: transparent; border: 0; cursor: pointer;
}
.row[hidden] { display: none; }
.row + .row { border-top: 1px solid var(--line); }
.row:active { background: var(--teal-soft); }
.row.static { cursor: default; }
.row.static:active { background: transparent; }
.row-text { display: flex; flex-direction: column; flex: 1; min-width: 0; gap: 2px; }
.row-text strong { font-size: 15.5px; font-weight: 600; }
.row-text span { font-size: 13.5px; color: var(--muted); }
.row .chev { width: 18px; height: 18px; color: #A3ACB4; flex: none; }
.danger { color: var(--error); }
.accent { color: var(--teal-dark); }

/* Forms & Inputs */
.form, .fields { display: flex; flex-direction: column; gap: 14px; }
.field { display: flex; flex-direction: column; gap: 5px; min-width: 0; }
.field[hidden] { display: none; }
.field > label { font-size: 13.5px; font-weight: 600; color: var(--muted); padding-left: 2px; }

input[type="text"], select {
  font: inherit; font-size: 16.5px; color: var(--text); min-height: 48px; width: 100%;
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 0 14px;
  appearance: none; -webkit-appearance: none;
}
input[type="text"]:focus, select:focus {
  outline: none; border-color: var(--teal); box-shadow: 0 0 0 3px rgba(18,165,160,.16);
}
input[aria-invalid="true"] { border-color: var(--error); }
select {
  padding-right: 34px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 14 9'%3E%3Cpath d='M1 1l6 6 6-6' fill='none' stroke='%2366727E' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 13px center;
}
.input-row { display: grid; grid-template-columns: 1fr 96px; gap: 8px; }
.input-row.single { grid-template-columns: 1fr; }
.input-row.with-suffix { grid-template-columns: 1fr 32px; }
.suffix { align-self: center; color: var(--muted); font-weight: 600; text-align: center; }
.input-big { font-size: 28px !important; font-weight: 650; min-height: 60px !important; }
.select-inline { width: auto; min-width: 82px; min-height: 40px; font-size: 15px; }

.err { color: var(--error); font-size: 13px; padding-left: 2px; }
.err[hidden] { display: none; }
.hint { color: var(--muted); font-size: 13.5px; text-align: center; }
.hint:empty { display: none; }
.hint.bad { color: var(--error); font-weight: 600; }

.more-fields {
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 2px 14px;
}
.more-fields summary {
  min-height: 44px; display: flex; align-items: center; font-size: 14px; font-weight: 600;
  color: var(--teal-dark); cursor: pointer; list-style: none;
}
.more-fields summary::-webkit-details-marker { display: none; }
.more-fields summary::before { content: "+ "; margin-right: 4px; }
.more-fields[open] summary::before { content: "− "; }
.more-fields[open] .fields { padding: 4px 0 14px; }

.form-foot { display: flex; justify-content: center; }
.form-foot[hidden] { display: none; }

/* Buttons */
.actions { display: flex; gap: 10px; }
.actions .btn { flex: 1; margin: 0; }
.btn {
  min-height: 48px; padding: 0 22px; border: 0; border-radius: 12px; cursor: pointer;
  background: var(--navy); color: #fff; font-size: 15.5px; font-weight: 600;
  display: inline-flex; align-items: center; justify-content: center; text-decoration: none;
}
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--teal-dark); color: #fff; }
.btn.secondary { background: var(--surface); color: var(--navy); border: 1px solid var(--line); }
.btn.text { background: transparent; color: var(--muted); min-height: 40px; padding: 0 14px; font-size: 14.5px; }
.btn.text:active { background: var(--line); }
.btn.text.danger { color: var(--error); }

/* Converter */
.conv-units { display: grid; grid-template-columns: 1fr 44px 1fr; gap: 8px; align-items: end; }
.swap {
  width: 44px; height: 48px; border: 0; border-radius: 12px;
  background: var(--teal-soft); color: var(--teal-dark); display: grid; place-items: center; cursor: pointer;
}
.swap:active { background: #cfece9; }
.conv-units select { font-size: 14.5px; padding-left: 10px; }

/* Result card */
.result { background: var(--navy); color: #fff; border-radius: var(--radius); padding: 18px 20px; }
.result[hidden] { display: none; }
.result-name { font-size: 13.5px; color: #9FB5C9; }
.result-value { margin-top: 2px; overflow-wrap: anywhere; }
.result-value .num { font-size: 38px; font-weight: 700; line-height: 1.15; color: #fff; }
.result-value .unit { font-size: 18px; font-weight: 600; color: #5ED9D3; }
.result-expr { margin-top: 6px; font-size: 13px; color: #A9BCCD; overflow-wrap: anywhere; }
.result-others {
  list-style: none; margin: 12px 0 0; padding: 10px 0 0; border-top: 1px solid rgba(255,255,255,.12);
  display: grid; grid-template-columns: 1fr 1fr; gap: 6px 12px; font-size: 13.5px; color: #9FB5C9;
}
.result-others li span { color: #fff; font-weight: 600; }
.result-rows { margin: 12px 0 0; padding-top: 10px; border-top: 1px solid rgba(255,255,255,.12); display: grid; gap: 7px; }
.result-rows div { display: flex; justify-content: space-between; gap: 12px; font-size: 13.5px; }
.result-rows dt { color: #9FB5C9; }
.result-rows dd { margin: 0; color: #fff; font-weight: 600; text-align: right; }
.result-steps { margin-top: 10px; font-size: 13px; color: #C9D6E2; }
.result-steps summary { cursor: pointer; min-height: 28px; color: #5ED9D3; font-weight: 600; }
.result-steps ol { margin: 6px 0 0; padding-left: 18px; display: grid; gap: 4px; }
.result-note { margin-top: 10px; font-size: 12px; color: #8FA5B8; }

/* Camera */
.cam, .cam section { display: flex; flex-direction: column; gap: 12px; }
.cam section[hidden] { display: none; }
.cam-card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 28px 20px 22px; display: flex; flex-direction: column; align-items: center; gap: 14px; text-align: center;
}
.cam-lead { font-size: 15px; color: var(--muted); max-width: 30ch; }
.cam-actions { display: flex; flex-direction: column; gap: 10px; width: 100%; margin-top: 4px; }
.cam-card .quiet-note { margin-top: 2px; }
.notice-box { padding: 12px 14px; border-radius: 12px; font-size: 14px; }
.notice-box.error { background: #F8E7E6; color: #7C2B29; }
.notice-box[hidden] { display: none; }
.video-wrap { background: #000; border-radius: var(--radius); overflow: hidden; }
.video-wrap video { display: block; width: 100%; max-height: 58vh; object-fit: contain; }
.steps { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.steps button {
  min-height: 44px; border: 1px solid var(--line); border-radius: 12px;
  background: var(--surface); font-size: 14.5px; font-weight: 600; cursor: pointer;
}
.steps button b {
  display: inline-grid; place-items: center; width: 20px; height: 20px; border-radius: 50%;
  background: var(--line); font-size: 12px; margin-right: 6px;
}
.steps button[aria-pressed="true"] { border-color: var(--teal); background: var(--teal-soft); }
.steps button[aria-pressed="true"] b { background: var(--teal-dark); color: #fff; }
.instruction { font-size: 14px; font-weight: 600; text-align: center; color: var(--navy); }
.canvas-wrap {
  background: #0B1620; border-radius: var(--radius); overflow: hidden;
  touch-action: none; user-select: none; -webkit-user-select: none;
}
.canvas-wrap canvas { display: block; touch-action: none; margin: 0 auto; }
.tools { display: flex; gap: 8px; }
.chip-btn {
  flex: 1; min-height: 40px; padding: 0 10px; border: 1px solid var(--line); border-radius: 10px;
  background: var(--surface); font-weight: 600; font-size: 13.5px; cursor: pointer;
}
.chip-btn:active { background: var(--teal-soft); }
.file-in { display: none; }

/* Ruler */
.ruler-screen {
  display: flex; flex-direction: column; gap: 10px;
  height: calc(100dvh - var(--topbar-h) - var(--safe-top) - var(--nav-h) - var(--safe-bottom) - 96px);
  min-height: 340px;
}
.ruler-screen.calib { height: auto; min-height: 0; }
.ruler-banner { background: #FBF3E4; color: #6D4A12; border-radius: 12px; padding: 9px 12px; font-size: 13.5px; }
.ruler-banner[hidden] { display: none; }
.ruler-banner a { color: var(--teal-dark); font-weight: 700; }
.ruler-controls { display: flex; gap: 8px; }
.ruler-controls .segmented { flex: 1; }
.ruler-canvas {
  flex: 1; min-height: 0; border-radius: var(--radius); overflow: hidden;
  background: #FBF7E8; border: 1px solid var(--line); touch-action: none;
}
.ruler-canvas canvas { display: block; touch-action: none; }
.ruler-readout { text-align: center; }
.r-num { font-size: 36px; font-weight: 700; line-height: 1.1; color: var(--navy); }
.r-unit { font-size: 17px; font-weight: 600; color: var(--teal-dark); }
.r-others { font-size: 13.5px; color: var(--muted); }
.r-others.warn { color: #8A5A10; }
.calib-canvas { flex: none; height: clamp(220px, 38dvh, 380px); }
.calib-adjust { display: flex; gap: 10px; align-items: center; }
.calib-adjust .chip-btn { flex: none; width: 44px; min-height: 44px; border-radius: 50%; font-size: 18px; }
.calib-adjust input[type=range] { flex: 1; height: 44px; accent-color: var(--teal-dark); }
.howto.compact { margin: 0; padding-left: 20px; display: grid; gap: 4px; font-size: 14px; color: var(--muted); }

/* Bottom nav */
.bottom-nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: flex; justify-content: center;
  height: calc(var(--nav-h) + var(--safe-bottom));
  padding: 0 max(4px, env(safe-area-inset-left)) var(--safe-bottom) max(4px, env(safe-area-inset-right));
  background: rgba(255,255,255,.94); border-top: 1px solid var(--line);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
}
.bottom-nav a {
  flex: 1; max-width: 112px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  text-decoration: none; color: #7C8793; font-size: 11px; font-weight: 600;
}
.bottom-nav a .ico { width: 22px; height: 22px; }
.bottom-nav a.active { color: var(--teal-dark); }

/* Toast & Dialog */
.toast {
  position: fixed; left: 50%; bottom: calc(var(--nav-h) + var(--safe-bottom) + 14px); z-index: 50;
  transform: translate(-50%, 16px); opacity: 0; pointer-events: none;
  max-width: calc(100% - 32px); padding: 10px 16px; border-radius: 10px;
  background: var(--navy); color: #fff; font-size: 14px; transition: .18s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.scrim { position: fixed; inset: 0; z-index: 60; display: grid; place-items: center; padding: 24px; background: rgba(15,33,51,.45); }
.scrim[hidden] { display: none; }
.dialog { width: 100%; max-width: 320px; padding: 22px 20px 12px; background: var(--surface); border-radius: 20px; }
.dialog h2 { font-size: 18px; margin-bottom: 6px; }
.dialog p { font-size: 14.5px; color: var(--muted); }
.dlg-actions { display: flex; justify-content: flex-end; gap: 4px; margin-top: 14px; }

/* Responsive */
@media (min-width: 700px) {
  .tool-grid { grid-template-columns: repeat(3, 1fr); }
}
@media (max-width: 359px) {
  .input-row { grid-template-columns: 1fr 84px; }
  .result-value .num { font-size: 32px; }
}
@media (orientation: landscape) and (max-height: 520px) {
  :root { --nav-h: 48px; --topbar-h: 40px; }
  .bottom-nav a span { display: none; }
  #app:has(.ruler-screen:not(.calib)) { max-width: none; }
  .r-num { font-size: 28px; }
  .ruler-screen:not(.calib) {
    height: calc(100dvh - var(--topbar-h) - var(--nav-h) - 68px);
    display: grid; grid-template-columns: 1fr 270px; grid-template-rows: auto auto 1fr auto; column-gap: 12px; min-height: 0;
  }
  .ruler-banner { grid-column: 1 / 3; }
  .ruler-controls { grid-column: 2; grid-row: 2; flex-direction: column; }
  .ruler-canvas { grid-column: 1; grid-row: 2 / 5; }
  .ruler-readout { grid-column: 2; grid-row: 3; align-self: center; }
  .ruler-screen .actions { grid-column: 2; grid-row: 4; }
  .ruler-screen .actions .btn { font-size: 13.5px; padding: 0 8px; min-height: 40px; }
}
