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

:root {
  --bg-primary: #f5f5f7;
  --bg-secondary: #ffffff;
  --text-primary: #000000;
  --text-secondary: #666666;
  --text-tertiary: #999999;
  --border-color: #ddd;
  --border-light: #eee;
  --shadow: rgba(0, 0, 0, 0.08);
  --shadow-hover: rgba(0, 0, 0, 0.12);
  --overlay: rgba(0, 0, 0, 0.3);
  --ios-blue: #007aff;
  --ios-red: #ff3b30;
  --fill-bg: rgba(120, 120, 128, 0.12);
  --ios-ease-out: cubic-bezier(0.32, 0.72, 0, 1);

  /* Apple Typography Scale */
  --fs-title1: 28px;
  --fs-title2: 22px;
  --fs-title3: 20px;
  --fs-headline: 17px;
  --fs-body: 17px;
  --fs-callout: 16px;
  --fs-subheadline: 15px;
  --fs-footnote: 13px;
  --fs-caption1: 12px;
  --fs-caption2: 11px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg-primary: #1a1a1a;
    --bg-secondary: #2a2a2a;
    --text-primary: #ffffff;
    --text-secondary: #aaaaaa;
    --text-tertiary: #777777;
    --border-color: #444444;
    --border-light: #3a3a3c;
    --shadow: rgba(0, 0, 0, 0.2);
    --shadow-hover: rgba(0, 0, 0, 0.3);
    --overlay: rgba(0, 0, 0, 0.6);
    --ios-blue: #0a84ff;
    --ios-red: #ff453a;
    --fill-bg: rgba(118, 118, 128, 0.24);
  }
}

html,
body {
  overflow-x: hidden;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica,
    Arial, sans-serif;
  background-color: var(--bg-primary);
  color: var(--text-primary);
  font-size: var(--fs-body);
  min-height: 100vh;
  line-height: 1.4;
  transition: background-color 0.3s ease, color 0.3s ease;
  /* Prevent overscroll bounce on iOS PWA */
  overscroll-behavior-y: contain;
  /* Safe area for iPhone X and later */
  padding-top: calc(16px + env(safe-area-inset-top));
  padding-bottom: calc(16px + env(safe-area-inset-bottom));
  padding-left: calc(16px + env(safe-area-inset-left));
  padding-right: calc(16px + env(safe-area-inset-right));
}

/* Visual cue for offline mode */
.is-offline .app-header h1::after {
  content: " (Offline)";
  font-size: 12px;
  color: var(--ios-red);
  vertical-align: middle;
}

.app-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.app-header h1 {
  font-size: var(--fs-title1);
  font-weight: 700;
  margin: 0;
}

.icon-button {
  background: none;
  border: none;
  padding: 8px;
  color: var(--ios-blue);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: background 0.2s;
}

.icon-button:active {
  background: var(--fill-bg);
}

/* Last Scraped Timestamp */
#last-scraped {
  margin: 30px 0 10px 0;
  font-size: var(--fs-caption2);
  color: var(--text-tertiary);
  white-space: nowrap; /* Prevent wrapping */
  text-align: center;
}

/* Dynamic Island Notification */
#dynamic-island {
  position: fixed;
  top: calc(8px + env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%) translateY(-20px) scale(0.5);
  background: #000000;
  color: #ffffff;
  padding: 6px 12px;
  border-radius: 20px;
  z-index: 11000;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: var(--fs-caption1);
  font-weight: 600;
  opacity: 0;
  pointer-events: none;
  transition: 
    transform 0.5s var(--ios-ease-out), 
    opacity 0.3s ease,
    width 0.5s var(--ios-ease-out),
    padding 0.5s var(--ios-ease-out);
  box-shadow: 0 4px 15px rgba(0,0,0,0.4);
  min-width: 100px;
  justify-content: center;
  white-space: nowrap;
}

#dynamic-island.active {
  transform: translateX(-50%) translateY(0) scale(1);
  opacity: 1;
}

#dynamic-island .island-icon {
  font-size: 14px;
}

/* Loading Indicator */
#loading-indicator {
  display: none;
  position: fixed;
  top: 80px;
  left: 50%;
  transform: translateX(-50%);
  background-color: var(--bg-secondary);
  padding: 10px;
  border-radius: 50%;
  z-index: 10000;
  pointer-events: none;
  box-shadow: 0 4px 12px var(--shadow);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.2s, transform 0.2s;
  width: 44px; /* Fixed size for circular indicator */
  height: 44px;
}

#loading-indicator.success {
  background-color: var(--ios-blue); /* Use iOS blue for success */
  color: white;
}

#loading-indicator .ios-spinner {
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 1;
  transition: opacity 0.2s;
}

#loading-indicator.success .ios-spinner {
  opacity: 0;
  display: none; /* Hide spinner when success */
}

#loading-indicator .ios-checkmark {
  display: none;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) rotate(45deg); /* Rotate for checkmark shape */
  width: 10px; /* Adjust size */
  height: 20px; /* Adjust size */
  border: solid white; /* White checkmark */
  border-width: 0 3px 3px 0; /* Thickness */
}

#loading-indicator.success .ios-checkmark {
  display: block;
}

.ios-spinner {
  position: relative;
  width: 24px;
  height: 24px;
}

.ios-spinner div {
  position: absolute;
  width: 2px;
  height: 6px;
  background: var(--text-secondary);
  left: 11px;
  top: 0;
  transform-origin: 1px 12px;
  border-radius: 1px;
  animation: ios-spinner-fade 1s linear infinite;
}

.ios-spinner div:nth-child(1) { transform: rotate(0deg); animation-delay: -1s; }
.ios-spinner div:nth-child(2) { transform: rotate(45deg); animation-delay: -0.875s; }
.ios-spinner div:nth-child(3) { transform: rotate(90deg); animation-delay: -0.75s; }
.ios-spinner div:nth-child(4) { transform: rotate(135deg); animation-delay: -0.625s; }
.ios-spinner div:nth-child(5) { transform: rotate(180deg); animation-delay: -0.5s; }
.ios-spinner div:nth-child(6) { transform: rotate(225deg); animation-delay: -0.375s; }
.ios-spinner div:nth-child(7) { transform: rotate(270deg); animation-delay: -0.25s; }
.ios-spinner div:nth-child(8) { transform: rotate(315deg); animation-delay: -0.125s; }

@keyframes ios-spinner-fade {
  from { opacity: 1; }
  to { opacity: 0.2; }
}

.modal {
  visibility: hidden; /* Use visibility for smoother transitions */
  opacity: 0;
  position: fixed;
  background-color: var(--overlay);
}

.modal.active { /* When active, slide up from bottom */
  visibility: visible;
  opacity: 1;
  display: flex; /* Use flexbox to position content at bottom */
  align-items: flex-end;
  justify-content: center;
  inset: 0; /* Shorthand for top, right, bottom, left */
  transition: opacity 0.3s ease-out;
  z-index: 10001; /* Ensure modal is above other content */
  backdrop-filter: blur(4px); /* Reduced backdrop blur to focus on modal glass */
  -webkit-backdrop-filter: blur(4px);
}

.modal-content {
  background: color-mix(in srgb, var(--bg-secondary), transparent 15%);
  padding: 20px;
  border-radius: 12px 12px 0 0; /* Rounded top corners for slide-up modal */
  width: 90%;
  max-width: 500px;
  max-height: 70vh;
  overflow-y: auto;
  color: var(--text-primary);
  box-shadow: 0 -5px 20px var(--shadow); /* Shadow for depth */
  transform: translateY(100%); /* Start off-screen */
  transition: transform 0.4s var(--ios-ease-out);
  padding-bottom: calc(20px + env(safe-area-inset-bottom)); /* Adjust for safe area */
  backdrop-filter: blur(20px) saturate(180%);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  border: 1px solid var(--separator);
  border-bottom: none;
}

.modal.active .modal-content {
  transform: translateY(0); /* Slide into view */
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.modal-header h2 {
  margin: 0;
  font-size: var(--fs-title2);
}

.close-btn {
  font-size: 28px;
  font-weight: bold;
  cursor: pointer;
  color: var(--text-tertiary);
}

.close-btn:hover {
  color: var(--text-primary);
}

.stock-input-group {
  margin-bottom: 15px;
  padding: 12px;
  border: 1px solid var(--border-light); /* Lighter border */
  border-radius: 10px; /* Slightly more rounded */
  background-color: var(--bg-primary); /* Match input background */
}

.stock-input-group input {
  width: 100%;
  padding: 10px; /* More padding */
  margin-bottom: 8px;
  border: 1px solid var(--border-color); /* Subtle border */
  border-radius: 8px; /* More rounded */
  font-size: var(--fs-body);
  background-color: var(--bg-primary);
  color: var(--text-primary);
  -webkit-appearance: none; /* Remove default iOS input styles */
  -moz-appearance: none;
  appearance: none;
}

.stock-input-group label {
  display: block;
  font-size: var(--fs-caption1);
  color: var(--text-secondary);
  margin-bottom: 4px;
}

.btn {
  padding: 10px 16px;
  border: none;
  border-radius: 6px;
  border-radius: 10px; /* More rounded buttons */
  cursor: pointer;
  font-size: var(--fs-headline);
  font-weight: 500; /* Slightly lighter font weight */
  transition: background-color 0.2s ease, transform 0.1s ease;
}

.btn:active {
  transform: scale(0.98); /* Subtle press effect */
}

.btn-primary {
  background-color: var(--ios-blue); /* iOS blue */
  color: white;
  margin-right: 10px;
  box-shadow: 0 2px 5px rgba(0, 122, 255, 0.2); /* Subtle blue shadow */
}

.btn-primary:hover {
  background-color: color-mix(in srgb, var(--ios-blue) 90%, black); /* Slightly darker on hover */
}

.btn-secondary {
  background-color: var(--bg-primary);
  color: var(--text-primary);
  border: 1px solid var(--border-light); /* Subtle border */
}

.btn-secondary:hover {
  background-color: var(--border-color); /* Slightly darker on hover */
  box-shadow: none;
}

.btn-danger {
  background-color: #ff3b30;
  color: white;
  font-size: 12px;
  padding: 6px 12px;
}
.btn-danger:hover {
  background-color: color-mix(in srgb, var(--ios-red) 90%, black); /* Slightly darker on hover */
  box-shadow: none;
}

.modal-actions {
  display: flex;
  gap: 10px;
  margin-top: 20px;
  justify-content: flex-end;
}

.add-stock-btn {
  display: block;
  width: 100%;
  margin-bottom: 20px;
}

.stocks-container {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px; /* Slightly smaller gap */
}

.stock-swipe-wrapper {
  position: relative;
  background-color: var(--ios-red);
  border-radius: 14px;
  overflow: hidden;
}

.stock-swipe-action {
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  width: 100px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: 600;
  z-index: 1;
}

.stock-card {
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-rows: auto auto auto;
  gap: 6px 12px;
  background: var(--bg-secondary);
  border-radius: 14px; /* Slightly more rounded corners */
  padding: 16px;
  box-shadow: 0 2px 8px var(--shadow); /* Softer, more diffused shadow */
  transition: transform 0.4s var(--ios-ease-out), box-shadow 0.2s ease;
  position: relative;
  z-index: 2;
}
.stock-card:hover {
  box-shadow: 0 4px 12px var(--shadow-hover); /* Slightly more pronounced on hover */
}

.stock-card:active {
  transform: scale(0.96);
  /* Snappy response on press, uses the base 0.4s ease-out for a springy recovery on release */
  transition: transform 0.1s ease-out;
  box-shadow: 0 2px 4px var(--shadow);
}

.stock-symbol {
  grid-column: 1;
  grid-row: 1;
  font-size: var(--fs-title3);
  font-weight: 600;
  color: var(--text-primary);
}

.stock-name {
  grid-column: 1;
  grid-row: 2;
  font-size: var(--fs-footnote);
  color: var(--text-secondary);
  text-overflow: ellipsis;
  overflow: hidden;
}

.stock-profitloss {
  grid-column: 2;
  grid-row: 1 / 3;
  align-self: start;
  text-align: right; /* Align to right */
  font-size: var(--fs-title3);
  font-weight: 600;
  color: var(--text-primary);
}

.pu-progress-container {
  width: 100%;
  height: 8px;
  background-color: var(--fill-bg);
  border-radius: 4px;
  margin: 12px 0;
  overflow: hidden;
}

.pu-progress-bar {
  height: 100%;
  width: 0%;
  background-color: var(--ios-blue);
  transition: width 0.4s var(--ios-ease-out), background-color 0.3s ease;
}

.stock-price {
  grid-column: 2;
  grid-row: 3;
  align-self: start;
  text-align: right; /* Align to right */
  font-size: var(--fs-callout);
  font-weight: 500;
  color: var(--text-secondary);
}

.stock-change {
  grid-column: 1;
  grid-row: 3;
  font-size: var(--fs-subheadline);
  font-weight: 500;
}

.change-positive {
  color: #34c759; /* iOS green */
}

.change-negative {
  color: #ff3b30; /* iOS red */
}

@media (max-width: 600px) {
  .stock-card {
    padding: 14px;
  }
  .stock-symbol {
    font-size: var(--fs-headline);
  }
  .stock-price {
    font-size: var(--fs-headline);
  }
  .stock-change {
    font-size: var(--fs-footnote);
  }
}

/* Tab Bar */
.tab-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: calc(50px + env(safe-area-inset-bottom));
  background-color: var(--bg-secondary);
  border-top: 1px solid var(--border-color);
  display: flex;
  justify-content: space-around;
  align-items: center;
  padding-bottom: env(safe-area-inset-bottom);
  z-index: 1000;
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
}

.tab-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  color: var(--text-secondary);
  font-size: 10px;
  font-weight: 500;
  gap: 4px;
  flex: 1;
  height: 50px;
  cursor: pointer;
}

.tab-item svg {
  transition: transform 0.2s var(--ios-ease-out), stroke-width 0.2s ease;
}

.tab-item.active {
  color: var(--ios-blue);
}

.tab-item.active svg {
  stroke-width: 2.5px; /* Thicker weight mirrors SF Symbol 'bold' variant */
  animation: ios-tab-bounce 0.4s var(--ios-ease-out);
}

@keyframes ios-tab-bounce {
  0% { transform: scale(1); }
  40% { transform: scale(0.82); }
  70% { transform: scale(1.08); }
  100% { transform: scale(1); }
}

/* Side Menu */
.menu-backdrop {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
  z-index: 1500;
  backdrop-filter: blur(2px);
}

.menu-backdrop.open {
  opacity: 1;
  pointer-events: auto;
}

.side-menu {
  position: fixed;
  top: 0;
  left: 0;
  width: 80%;
  max-width: 320px;
  height: 100%;
  background: color-mix(in srgb, var(--bg-secondary), transparent 15%);
  box-shadow: 2px 0 10px rgba(0,0,0,0.1);
  z-index: 1600;
  transform: translateX(-100%);
  transition: transform 0.4s var(--ios-ease-out);
  display: flex;
  flex-direction: column;
  padding: calc(16px + env(safe-area-inset-top)) 24px calc(16px + env(safe-area-inset-bottom)) 24px;
  backdrop-filter: blur(20px) saturate(180%);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  border-right: 1px solid var(--separator);
}

.side-menu.open {
  transform: translateX(0);
}

.menu-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
}

.menu-header h3 {
  margin: 0;
  font-size: 24px;
  font-weight: 700;
}

.menu-content {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.menu-action-btn {
  width: 100%;
  padding: 14px;
  border-radius: 12px;
  border: none;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  background-color: var(--fill-bg);
  color: var(--text-primary);
  transition: background 0.2s;
  text-align: left;
}

.menu-action-btn.primary {
  background-color: var(--ios-blue);
  color: white;
  text-align: center;
}

/* Navigation Stack Animations */
.view-push-in {
  animation: iosPushIn 0.35s forwards cubic-bezier(0.4, 0, 0.2, 1);
  position: fixed !important;
  inset: 0;
  z-index: 1100;
  background: var(--bg-primary);
  padding: 16px;
  padding-top: calc(16px + env(safe-area-inset-top));
}

.view-pop-out {
  animation: iosPopOut 0.4s forwards var(--ios-ease-out);
  position: fixed !important;
  inset: 0;
  z-index: 1101;
  background: var(--bg-primary);
  padding: 16px;
  padding-top: calc(16px + env(safe-area-inset-top));
}

@keyframes iosPushIn {
  from { transform: translateX(100%); box-shadow: -10px 0 20px rgba(0,0,0,0.1); }
  to { transform: translateX(0); }
}

@keyframes iosPopOut {
  from { transform: translateX(0); }
  to { transform: translateX(100%); box-shadow: -10px 0 20px rgba(0,0,0,0.1); }
}
