/* ========================================
   Stories Viewer PWA - Styles
   ======================================== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { scroll-behavior: smooth; -webkit-text-size-adjust: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  font-size: 16px; line-height: 1.5;
  background: var(--bg); color: var(--text);
  overflow-x: hidden;
  min-height: 100vh; min-height: 100dvh;
}
a { text-decoration: none; color: inherit; }
button { border: none; background: none; cursor: pointer; font: inherit; color: inherit; }
input { font: inherit; color: inherit; }
img { max-width: 100%; display: block; }

:root {
  --bg: #0a0a0a; --bg2: #161618; --bg3: #1c1c1e;
  --text: #e8e8e8; --muted: #7d8088; --border: #2a2a2c;
  --ig1: #f09433; --ig2: #e6683c; --ig3: #dc2743; --ig4: #cc2366; --ig5: #bc1888;
  --accent: #dc2743;
  --r: 12px; --rs: 8px;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
}
body.light {
  --bg: #fafafa; --bg2: #fff; --bg3: #f0f0f2;
  --text: #262626; --muted: #8e8e8e; --border: #dbdbdb;
}

/* ---------- View container ---------- */
.view { min-height: 100vh; min-height: 100dvh; }

/* =============================================
   LOGIN VIEW
   ============================================= */
.login-container {
  max-width: 420px; margin: 0 auto;
  padding: 60px 24px 40px; text-align: center;
}
.login-logo { margin-bottom: 16px; }
.login-title { font-size: 24px; font-weight: 700; }
.login-subtitle { font-size: 14px; color: var(--muted); margin-bottom: 28px; }

.login-form { text-align: left; }
.field { margin-bottom: 16px; }
.field label {
  display: block; font-size: 13px; font-weight: 600;
  color: var(--muted); margin-bottom: 5px; text-transform: uppercase; letter-spacing: .4px;
}
.field .req { color: #dc2743; }
.field .opt { color: var(--muted); font-weight: 400; text-transform: none; }
.field input {
  width: 100%; padding: 12px 14px;
  background: var(--bg3); border: 1px solid var(--border); border-radius: var(--rs);
  font-size: 15px; color: var(--text);
  transition: border-color .2s;
  appearance: none; -webkit-appearance: none;
}
.field input:focus { outline: none; border-color: var(--accent); }
.field input::placeholder { color: var(--muted); opacity: .5; }
.hint { display: block; font-size: 12px; color: var(--muted); margin-top: 4px; }

/* Tabs */
.login-tabs {
  display: flex; gap: 0; margin-bottom: 20px;
  border: 1px solid var(--border); border-radius: var(--rs); overflow: hidden;
}
.login-tabs .tab {
  flex: 1; padding: 10px 0; font-size: 14px; font-weight: 600;
  background: var(--bg3); color: var(--muted); text-align: center;
  transition: all .2s; border: none; cursor: pointer;
}
.login-tabs .tab.active {
  background: var(--accent); color: #fff;
}
.tab-content[hidden] { display: none; }

/* Textarea */
.field textarea {
  width: 100%; padding: 12px 14px;
  background: var(--bg3); border: 1px solid var(--border); border-radius: var(--rs);
  font-size: 13px; font-family: 'Courier New', monospace; color: var(--text);
  resize: vertical; line-height: 1.5;
  appearance: none; -webkit-appearance: none;
}
.field textarea:focus { outline: none; border-color: var(--accent); }
.field textarea::placeholder { color: var(--muted); opacity: .5; }

/* Parsed info */
.parsed-info {
  display: flex; align-items: center; gap: 6px;
  padding: 8px 12px; margin-bottom: 12px;
  background: rgba(46, 204, 113, .1); border: 1px solid rgba(46, 204, 113, .3);
  border-radius: var(--rs); font-size: 13px; color: #2ecc71;
}
.parsed-ok { font-size: 16px; }

.btn-primary {
  width: 100%; padding: 14px; margin-top: 8px;
  background: linear-gradient(135deg, var(--ig1), var(--ig3), var(--ig5));
  color: #fff; font-size: 16px; font-weight: 600;
  border-radius: var(--rs); transition: opacity .2s;
}
.btn-primary:active { opacity: .85; }
.btn-primary:disabled { opacity: .5; pointer-events: none; }

.btn-small {
  padding: 8px 20px; font-size: 14px; font-weight: 600;
  background: var(--bg3); border: 1px solid var(--border); border-radius: var(--rs);
  color: var(--text); margin-top: 12px;
}

.loader {
  display: inline-block; width: 18px; height: 18px;
  border: 2px solid rgba(255,255,255,.3); border-top-color: #fff;
  border-radius: 50%; animation: spin .6s linear infinite; vertical-align: middle;
}
.loader-big {
  width: 36px; height: 36px; margin: 0 auto 12px;
  border: 3px solid var(--border); border-top-color: var(--accent);
  border-radius: 50%; animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.form-error {
  text-align: center; color: #ff4d6a; font-size: 14px; margin-top: 12px;
}

.help-section {
  text-align: left; margin-top: 24px; font-size: 14px; color: var(--muted);
}
.help-section summary {
  cursor: pointer; font-weight: 600; color: var(--text); padding: 8px 0;
}
.help-section ol { padding-left: 20px; margin-top: 8px; line-height: 1.8; }
.help-section code {
  background: var(--bg3); padding: 2px 6px; border-radius: 4px; font-size: 13px;
}

/* =============================================
   APP VIEW
   ============================================= */
.app-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 10px 16px; position: sticky; top: 0; z-index: 100;
  background: var(--bg); border-bottom: 1px solid var(--border);
}
.app-title {
  font-size: 22px; font-weight: 700;
  background: linear-gradient(135deg, var(--ig1), var(--ig5));
  -webkit-background-clip: text; -webkit-text-fill-color: transparent;
  background-clip: text;
}
.header-actions { display: flex; gap: 6px; }
.btn-icon {
  width: 38px; height: 38px; display: flex; align-items: center; justify-content: center;
  border-radius: 50%; background: var(--bg3); color: var(--muted); transition: all .2s;
}
.btn-icon:active { background: var(--border); color: var(--text); }
.btn-danger:active { color: #ff4d6a; }

.loading {
  display: flex; flex-direction: column; align-items: center;
  justify-content: center; padding: 80px 20px; color: var(--muted);
}

.content { padding-bottom: calc(20px + var(--safe-b)); }

/* ---------- Stories Tray ---------- */
.tray-scroll {
  display: flex; gap: 14px; padding: 14px 16px;
  overflow-x: auto; -webkit-overflow-scrolling: touch; scrollbar-width: none;
}
.tray-scroll::-webkit-scrollbar { display: none; }

.tray-item {
  flex-shrink: 0; display: flex; flex-direction: column; align-items: center;
  gap: 5px; width: 74px; cursor: pointer;
}
.tray-ring {
  width: 68px; height: 68px; border-radius: 50%; padding: 3px;
  background: linear-gradient(135deg, var(--ig1), var(--ig3), var(--ig5));
}
.tray-ring.seen { background: var(--border); }
.tray-avatar {
  width: 100%; height: 100%; border-radius: 50%;
  border: 3px solid var(--bg); object-fit: cover;
  background: var(--bg3); color: var(--muted);
  display: flex; align-items: center; justify-content: center;
  font-size: 18px; font-weight: 700; text-transform: uppercase;
}
.tray-name {
  font-size: 11px; color: var(--muted); text-align: center;
  max-width: 74px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

/* ---------- Friends List ---------- */
.section-title { font-size: 16px; font-weight: 700; padding: 12px 16px 6px; }
.friends-list { padding: 0 8px; }
.friend-row {
  display: flex; align-items: center; gap: 12px;
  padding: 8px 12px; border-radius: var(--r);
  transition: background .15s; cursor: pointer;
}
.friend-row:active { background: var(--bg3); }
.friend-pic {
  width: 50px; height: 50px; border-radius: 50%;
  border: 2px solid var(--border); object-fit: cover; flex-shrink: 0; background: var(--bg3);
}
.friend-pic.has-story { border-color: var(--ig3); }
.friend-info { flex: 1; min-width: 0; }
.friend-name {
  font-size: 15px; font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.friend-sub { font-size: 12px; color: var(--muted); }
.friend-count {
  font-size: 12px; color: var(--muted); background: var(--bg3);
  padding: 2px 8px; border-radius: 10px; flex-shrink: 0;
}

/* ---------- States ---------- */
.empty-state, .error-state {
  display: flex; flex-direction: column; align-items: center;
  justify-content: center; padding: 60px 20px; color: var(--muted); gap: 8px;
}

/* =============================================
   STORY VIEWER
   ============================================= */
.story-viewer {
  position: fixed; inset: 0; z-index: 1000; background: #000;
  display: none; flex-direction: column; animation: svIn .25s ease;
}
.story-viewer.active {
  display: flex;
}
@keyframes svIn { from { opacity: 0; } to { opacity: 1; } }

.sv-progress {
  position: absolute; top: calc(4px + var(--safe-t));
  left: 8px; right: 8px; z-index: 11; display: flex; gap: 3px;
}
.pbar { flex: 1; height: 2px; background: rgba(255,255,255,.3); border-radius: 2px; overflow: hidden; }
.pfill { height: 100%; background: #fff; width: 0%; transition: width .1s linear; }

.sv-header {
  position: absolute; top: 0; left: 0; right: 0; z-index: 10;
  display: flex; justify-content: space-between; align-items: center;
  padding: 16px; padding-top: calc(16px + var(--safe-t));
  background: linear-gradient(180deg, rgba(0,0,0,.6), transparent);
}
.sv-user { display: flex; align-items: center; gap: 10px; }
.sv-avatar { width: 34px; height: 34px; border-radius: 50%; border: 2px solid #fff; object-fit: cover; }
.sv-name { font-size: 14px; font-weight: 600; color: #fff; }
.sv-time { font-size: 11px; color: rgba(255,255,255,.6); }
.sv-close { width: 36px; height: 36px; font-size: 26px; color: #fff; display: flex; align-items: center; justify-content: center; }

.sv-body {
  flex: 1; display: flex; align-items: center; justify-content: center; overflow: hidden;
}
.sv-body img, .sv-body video { max-width: 100%; max-height: 100%; object-fit: contain; }

.sv-nav { position: absolute; inset: 0; z-index: 5; display: flex; }
.sv-prev, .sv-next { flex: 1; background: transparent; }

.sv-download {
  position: absolute; bottom: calc(20px + var(--safe-b)); right: 16px; z-index: 12;
  width: 44px; height: 44px; display: flex; align-items: center; justify-content: center;
  background: rgba(0,0,0,.5); border-radius: 50%; backdrop-filter: blur(8px);
}

/* ---------- Responsive ---------- */
@media (min-width: 768px) {
  #app-view .content, .app-header { max-width: 500px; margin-left: auto; margin-right: auto; }
  .login-container { max-width: 440px; }
}
