:root {
  --bg: #0b0f19;
  --panel: #111827;
  --panel-alt: #1f2937;
  --border: #374151;
  --text: #f3f4f6;
  --muted: #9ca3af;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --success: #10b981;
  --success-hover: #059669;
  --warning: #f59e0b;
  --warning-hover: #d97706;
  --danger: #ef4444;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  overflow-x: hidden;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
}

body {
  padding-bottom: calc(1.75rem + env(safe-area-inset-bottom, 0px));
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.is-hidden {
  display: none !important;
}

.mt-xs {
  margin-top: 0.3rem;
}

.mt-sm {
  margin-top: 0.5rem;
}

.no-margin {
  margin: 0;
}

.card-divider {
  border: 0;
  border-top: 1px solid var(--border);
  width: 100%;
  margin: 0.15rem 0;
}

/* Header */
header {
  position: sticky;
  top: 0;
  z-index: 50;
  background: rgba(15, 23, 42, 0.94);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  padding: calc(0.8rem + env(safe-area-inset-top, 0px)) 1.5rem 0.8rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.65rem 1rem;
}

.header-title-wrap {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.6rem;
  min-width: 0;
}

header h1 {
  margin: 0;
  font-size: 1.14rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 0.45rem;
  line-height: 1.3;
}

.header-status-wrap {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-width: 0;
}

.header-mobile-sub {
  display: none;
}

.badge {
  display: inline-flex;
  align-items: center;
  background: rgba(16, 185, 129, 0.15);
  color: #6ee7b7;
  border: 1px solid rgba(16, 185, 129, 0.4);
  padding: 0.22rem 0.62rem;
  border-radius: 999px;
  font-size: 0.73rem;
  font-weight: 600;
  white-space: nowrap;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  flex-shrink: 0;
}

.badge.warn {
  background: rgba(245, 158, 11, 0.15);
  color: #fcd34d;
  border-color: rgba(245, 158, 11, 0.4);
}

.badge.err {
  background: rgba(239, 68, 68, 0.15);
  color: #fca5a5;
  border-color: rgba(239, 68, 68, 0.4);
}

/* Section Filter Pills (Tablet & Landscape Phone) */
.mobile-tabs {
  display: none;
  gap: 0.45rem;
  overflow-x: auto;
  padding: 0.6rem 1.25rem;
  background: #0f172a;
  border-bottom: 1px solid var(--border);
  scrollbar-width: none;
  align-items: center;
}

.mobile-tabs::-webkit-scrollbar {
  display: none;
}

.tab-pill {
  flex: 0 0 auto;
  background: var(--panel-alt);
  color: var(--muted);
  border: 1px solid var(--border);
  padding: 0.45rem 0.9rem;
  border-radius: 999px;
  font-size: 0.82rem;
  font-weight: 600;
  min-height: 40px;
}

.tab-pill:hover {
  background: #283548;
  color: #e5e7eb;
}

.tab-pill.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

/* Main Grid */
main {
  max-width: 1440px;
  margin: 1.25rem auto;
  padding: 0 1.25rem;
  display: grid;
  grid-template-columns: repeat(12, minmax(0, 1fr));
  gap: 1.25rem;
  align-items: stretch;
}

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 0.85rem;
  padding: 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
  min-width: 0;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
}

.col-6 {
  grid-column: span 6;
}

.col-12 {
  grid-column: span 12;
}

h2 {
  margin: 0;
  font-size: 1rem;
  color: #e5e7eb;
  border-bottom: 1px solid var(--border);
  padding-bottom: 0.65rem;
  min-height: 2.35rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.4rem 0.6rem;
}

h2 > span:first-child {
  min-width: 0;
}

label {
  display: block;
  font-size: 0.8rem;
  color: var(--muted);
  margin-bottom: 0.32rem;
  font-weight: 500;
  line-height: 1.35;
}

label code,
.small-note code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.74rem;
  background: rgba(255, 255, 255, 0.07);
  color: #cbd5e1;
  padding: 0.06rem 0.32rem;
  border-radius: 0.25rem;
}

input, select, textarea {
  width: 100%;
  background: var(--panel-alt);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.6rem 0.8rem;
  border-radius: 0.55rem;
  font-size: 0.92rem;
  font-family: inherit;
  transition: border-color 0.15s, box-shadow 0.15s;
}

input, select {
  height: 44px;
  min-height: 44px;
}

select {
  appearance: none;
  -webkit-appearance: none;
  padding-right: 2.25rem;
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%239ca3af' stroke-width='2.2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 0.75rem center;
  background-size: 16px 16px;
}

input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.2);
}

textarea {
  min-height: 112px;
  resize: vertical;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.82rem;
  line-height: 1.45;
}

#kvkkText {
  min-height: 132px;
}

.plain-form {
  margin: 0;
  width: 100%;
}

/* Form Rows & Alignment */
.row {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.75rem;
  align-items: end;
}

.row.row-3 {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.row > div {
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  min-width: 0;
}

.inline-action-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 0.75rem;
  align-items: center;
  margin: 0;
}

/* Buttons */
.btn-group {
  display: flex;
  flex-wrap: wrap;
  gap: 0.55rem;
}

.btn-group > button,
.btn-group > .file-btn {
  flex: 1 1 auto;
}

button, .file-btn {
  background: var(--accent);
  color: white;
  border: none;
  padding: 0.62rem 0.95rem;
  min-height: 44px;
  margin: 0;
  border-radius: 0.55rem;
  font-weight: 600;
  cursor: pointer;
  font-size: 0.85rem;
  line-height: 1.25;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  text-align: center;
  white-space: nowrap;
  transition: background 0.15s, transform 0.08s;
}

button:active, .file-btn:active {
  transform: scale(0.985);
}

button:hover {
  background: var(--accent-hover);
}

button.secondary, .file-btn {
  background: #374151;
}

button.secondary:hover, .file-btn:hover {
  background: #4b5563;
}

button.success {
  background: var(--success);
}

button.success:hover {
  background: var(--success-hover);
}

button.warning {
  background: var(--warning);
  color: #111827;
}

button.warning:hover {
  background: var(--warning-hover);
}

/* Output Terminal */
.output-wrap {
  margin-top: auto;
  padding-top: 0.2rem;
  display: flex;
  flex-direction: column;
  gap: 0.38rem;
  min-width: 0;
}

.output-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.76rem;
  color: var(--muted);
}

.mini-btn {
  flex: 0 0 auto;
  min-height: 32px;
  padding: 0.25rem 0.65rem;
  font-size: 0.74rem;
  border-radius: 0.42rem;
  background: #1f2937;
  border: 1px solid var(--border);
  color: #d1d5db;
}

.mini-btn:hover {
  background: #374151;
}

pre {
  background: #070a12;
  border: 1px solid #1f2937;
  border-radius: 0.55rem;
  padding: 0.85rem;
  margin: 0;
  overflow-x: auto;
  font-size: 0.78rem;
  color: #a7f3d0;
  min-height: 76px;
  max-height: 320px;
  white-space: pre-wrap;
  word-break: break-word;
  max-width: 100%;
}

.status-box {
  background: rgba(16, 185, 129, 0.08);
  border: 1px solid rgba(16, 185, 129, 0.35);
  border-radius: 0.6rem;
  padding: 0.85rem 1rem;
  font-size: 0.85rem;
  word-break: break-word;
}

.status-box.error {
  background: rgba(239, 68, 68, 0.1);
  border-color: rgba(239, 68, 68, 0.4);
  color: #fecaca;
}

.fingerprint-pill {
  display: inline-block;
  margin-top: 0.4rem;
  padding: 0.45rem 0.8rem;
  border-radius: 0.5rem;
  background: #0f172a;
  border: 1px dashed #38bdf8;
  color: #38bdf8;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-weight: 700;
  font-size: 0.95rem;
  letter-spacing: 0.04em;
}

.small-note {
  font-size: 0.76rem;
  color: var(--muted);
}

/* Bottom Navigation Bar for Mobile Phones */
.mobile-bottom-nav {
  display: none;
}

/* Medium Desktop / Laptop (1025px - 1280px) */
@media (min-width: 1025px) and (max-width: 1280px) {
  .row.row-3 {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  .row.row-3 > div:nth-child(3) {
    grid-column: 1 / -1;
  }
  button, .file-btn {
    white-space: normal;
  }
}

/* Touch Devices (iOS / iPadOS / Android): prevent focus auto-zoom */
@media (hover: none) and (pointer: coarse) {
  input, select, textarea {
    font-size: 16px;
  }
}

/* Tablet & Single-Column Breakpoint (<= 1024px) */
@media (max-width: 1024px) {
  .col-6 {
    grid-column: span 12;
  }
  .mobile-tabs {
    display: flex;
  }
  .card.is-hidden-tab {
    display: none;
  }
  input, select {
    font-size: 16px;
    height: 46px;
    min-height: 46px;
  }
  textarea {
    font-size: 15px;
  }
}

/* Mobile Portrait & Narrow Screens (<= 640px) */
@media (max-width: 640px) {
  body {
    padding-bottom: calc(4.85rem + env(safe-area-inset-bottom, 0px));
  }
  header {
    padding: calc(0.6rem + env(safe-area-inset-top, 0px)) 0.9rem 0.65rem;
    gap: 0.45rem;
  }
  .header-title-wrap {
    width: 100%;
    justify-content: space-between;
  }
  header h1 {
    font-size: 0.98rem;
  }
  .header-protocol-badge {
    display: none;
  }
  .header-status-wrap {
    width: 100%;
    justify-content: space-between;
  }
  .header-mobile-sub {
    display: inline-flex;
  }
  #globalSessionStatus {
    flex: 1 1 auto;
    justify-content: center;
    padding: 0.26rem 0.6rem;
  }
  .mobile-tabs {
    display: none;
  }
  main {
    margin: 0.8rem auto;
    padding: 0 0.75rem;
    gap: 0.9rem;
  }
  .card {
    padding: 0.95rem;
    border-radius: 0.75rem;
    gap: 0.8rem;
  }
  h2 {
    font-size: 0.95rem;
    min-height: 0;
    padding-bottom: 0.55rem;
  }
  textarea {
    font-size: 16px;
  }
  .row,
  .row.row-3,
  .inline-action-row {
    grid-template-columns: 1fr;
    gap: 0.65rem;
  }
  .btn-group {
    flex-direction: column;
    gap: 0.5rem;
  }
  .btn-group > button,
  .btn-group > .file-btn,
  .inline-action-row > button {
    width: 100%;
    min-height: 46px;
    font-size: 0.88rem;
    white-space: normal;
  }
  .mini-btn {
    min-height: 36px;
    padding: 0.3rem 0.7rem;
  }
  pre {
    font-size: 0.75rem;
    max-height: 260px;
    padding: 0.75rem;
  }
  .mobile-bottom-nav {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 60;
    display: grid;
    grid-template-columns: repeat(5, minmax(0, 1fr));
    background: rgba(15, 23, 42, 0.96);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border-top: 1px solid var(--border);
    padding: 0.35rem 0.25rem calc(0.35rem + env(safe-area-inset-bottom, 0px));
  }
  .mobile-bottom-nav button {
    background: transparent;
    color: var(--muted);
    border: none;
    min-height: 46px;
    padding: 0.2rem 0.1rem;
    border-radius: 0.5rem;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.15rem;
    font-size: 0.68rem;
    font-weight: 600;
    white-space: nowrap;
  }
  .mobile-bottom-nav button span.icon {
    font-size: 1.05rem;
    line-height: 1;
  }
  .mobile-bottom-nav button.active {
    color: #60a5fa;
    background: rgba(59, 130, 246, 0.14);
  }
}

/* Ultra-narrow phones (<= 359px, e.g. 320px iPhone SE) */
@media (max-width: 359px) {
  header h1 {
    font-size: 0.9rem;
  }
  .header-mobile-sub {
    display: none !important;
  }
  .mobile-bottom-nav button {
    font-size: 0.62rem;
  }
}

/* Short Landscape Phones (e.g. 667x375 / 844x390) */
@media (max-height: 520px) and (max-width: 1024px) {
  body {
    padding-bottom: 1.25rem;
  }
  header {
    position: static;
  }
  .mobile-bottom-nav {
    display: none;
  }
  .mobile-tabs {
    display: flex;
  }
}
