/* ===== CSS Variables ===== */
:root {
  --color-primary: #ff5500;
  --color-primary-dark: #e64d00;
  --color-bg: #f5f5f5;
  --color-white: #ffffff;
  --color-text: #333333;
  --color-text-secondary: #999999;
  --color-text-placeholder: #cccccc;
  --color-border: #eeeeee;
  --color-link: #4a90d9;
  --color-danger: #ff3b30;
  --header-height: 44px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --max-width: 480px;
  --font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Helvetica Neue", "Microsoft YaHei", sans-serif;
}

/* ===== Reset ===== */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: var(--font-family);
  background: var(--color-bg);
  color: var(--color-text);
  line-height: 1.5;
  min-height: 100vh;
  min-height: 100dvh;
  -webkit-font-smoothing: antialiased;
}

button, input, select {
  font-family: inherit;
  font-size: inherit;
  border: none;
  outline: none;
  background: none;
}

a {
  color: var(--color-link);
  text-decoration: none;
}

img {
  display: block;
  max-width: 100%;
}

/* ===== Page Layout ===== */
.page {
  max-width: var(--max-width);
  margin: 0 auto;
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--color-bg);
  position: relative;
}

/* Desktop: center with shadow */
@media (min-width: 481px) {
  body {
    background: #e8e8e8;
  }
  .page {
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
  }
}

/* ===== Header ===== */
.header {
  position: sticky;
  top: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: calc(var(--header-height) + var(--safe-top));
  padding: var(--safe-top) 12px 0;
  background: var(--color-primary);
  color: var(--color-white);
}

.header-left {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 80px;
}

.btn-back {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  color: var(--color-white);
  cursor: pointer;
  padding: 0;
}

.btn-close {
  font-size: 14px;
  color: var(--color-white);
  cursor: pointer;
  padding: 4px 0;
}

.header-title {
  font-size: 17px;
  font-weight: 600;
  text-align: center;
  flex: 1;
}

.btn-share {
  font-size: 14px;
  color: var(--color-white);
  cursor: pointer;
  min-width: 40px;
  text-align: right;
  padding: 4px 0;
}

/* ===== Main ===== */
.main {
  padding-bottom: calc(20px + var(--safe-bottom));
}

/* ===== Form Sections ===== */
.form-section {
  background: var(--color-white);
  margin-bottom: 10px;
  overflow: visible;
}

.form-row {
  display: flex;
  align-items: center;
  padding: 0 16px;
  min-height: 50px;
  border-bottom: 1px solid var(--color-border);
  position: relative;
}

.form-row:last-child {
  border-bottom: none;
}

.form-label {
  flex-shrink: 0;
  width: 80px;
  font-size: 15px;
  font-weight: 500;
  color: var(--color-text);
}

.form-input {
  flex: 1;
  height: 50px;
  font-size: 15px;
  color: var(--color-text);
  text-align: right;
  padding: 0;
}

.form-input::placeholder {
  color: var(--color-text-placeholder);
}

.form-input-code {
  padding-right: 100px;
}

.form-row-select {
  position: relative;
}

.form-row-stock {
  position: relative;
  overflow: visible;
  z-index: 10;
}

.stock-input-wrap {
  flex: 1;
  position: relative;
  min-width: 0;
}

.stock-input-wrap .form-input {
  width: 100%;
  text-align: right;
}

.stock-dropdown {
  position: absolute;
  top: 100%;
  left: -96px;
  right: -16px;
  background: var(--color-white);
  border: 1px solid var(--color-border);
  border-top: none;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  z-index: 200;
  max-height: 240px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.stock-dropdown[hidden] {
  display: none;
}

.stock-dropdown-header {
  display: flex;
  align-items: center;
  padding: 8px 12px;
  background: #fafafa;
  border-bottom: 1px solid var(--color-border);
  font-size: 12px;
  color: var(--color-text-secondary);
  flex-shrink: 0;
}

.stock-dropdown-header .col-code {
  width: 72px;
  flex-shrink: 0;
}

.stock-dropdown-header .col-name {
  width: 72px;
  flex-shrink: 0;
}

.stock-dropdown-header .col-period {
  flex: 1;
  text-align: right;
}

.stock-dropdown-list {
  list-style: none;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.stock-dropdown-item {
  display: flex;
  align-items: center;
  padding: 10px 12px;
  border-bottom: 1px solid var(--color-border);
  cursor: pointer;
  font-size: 13px;
  line-height: 1.4;
}

.stock-dropdown-item:last-child {
  border-bottom: none;
}

.stock-dropdown-item:active,
.stock-dropdown-item:hover {
  background: #f5f8ff;
}

.stock-dropdown-item .col-code {
  width: 72px;
  flex-shrink: 0;
  color: var(--color-link);
  font-weight: 500;
}

.stock-dropdown-item .col-name {
  width: 72px;
  flex-shrink: 0;
  color: var(--color-link);
}

.stock-dropdown-item .col-period {
  flex: 1;
  text-align: right;
  color: var(--color-text-secondary);
  font-size: 11px;
}

.stock-dropdown-empty {
  padding: 16px 12px;
  text-align: center;
  font-size: 13px;
  color: var(--color-text-secondary);
}

.form-select {
  flex: 1;
  height: 50px;
  font-size: 15px;
  color: var(--color-text);
  text-align: right;
  text-align-last: right;
  direction: rtl;
  padding-right: 20px;
  appearance: none;
  -webkit-appearance: none;
  background: transparent;
  cursor: pointer;
}

.form-select option {
  direction: ltr;
  text-align: left;
}

.form-select:invalid,
.form-select option[value=""][disabled] {
  color: var(--color-text-placeholder);
}

.select-arrow {
  position: absolute;
  right: 16px;
  top: 50%;
  transform: translateY(-50%);
  width: 0;
  height: 0;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-top: 6px solid var(--color-text-secondary);
  pointer-events: none;
}

/* SMS Button */
.btn-sms {
  position: absolute;
  right: 16px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 14px;
  color: var(--color-primary);
  cursor: pointer;
  white-space: nowrap;
  padding: 4px 0;
  border-left: 1px solid var(--color-border);
  padding-left: 12px;
}

.btn-sms:disabled {
  color: var(--color-text-secondary);
  cursor: not-allowed;
}

/* ===== Upload Section ===== */
.upload-section {
  background: var(--color-white);
  padding: 16px;
  margin-bottom: 10px;
}

.upload-title {
  font-size: 15px;
  font-weight: 500;
  color: var(--color-text);
  margin-bottom: 8px;
}

.upload-hint {
  font-size: 13px;
  color: var(--color-text-secondary);
  margin-bottom: 6px;
}

.upload-desc {
  font-size: 12px;
  color: var(--color-text-secondary);
  line-height: 1.6;
  margin-bottom: 16px;
}

.upload-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.upload-item {
  width: calc((100% - 20px) / 3);
  aspect-ratio: 1;
  border-radius: 4px;
  overflow: hidden;
  position: relative;
}

.upload-add {
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px dashed #dddddd;
  background: #fafafa;
  cursor: pointer;
}

.upload-plus {
  font-size: 36px;
  color: #dddddd;
  font-weight: 300;
  line-height: 1;
}

.upload-preview {
  border: 1px solid var(--color-border);
}

.upload-preview img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.upload-remove {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.5);
  color: white;
  font-size: 14px;
  line-height: 20px;
  text-align: center;
  cursor: pointer;
  border: none;
}

/* ===== Submit Area ===== */
.submit-area {
  padding: 20px 16px;
}

.btn-submit {
  display: block;
  width: 100%;
  height: 48px;
  background: var(--color-primary);
  color: var(--color-white);
  font-size: 17px;
  font-weight: 500;
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.2s;
}

.btn-submit:hover {
  background: var(--color-primary-dark);
}

.btn-submit:active {
  opacity: 0.9;
}

.btn-submit:disabled {
  background: #ffaa80;
  cursor: not-allowed;
}

.disclaimer {
  margin-top: 14px;
  font-size: 12px;
  color: var(--color-text-secondary);
  line-height: 1.7;
  text-align: justify;
}

/* ===== Toast ===== */
.toast {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: rgba(0, 0, 0, 0.75);
  color: white;
  padding: 12px 24px;
  border-radius: 8px;
  font-size: 14px;
  z-index: 1000;
  opacity: 0;
  transition: opacity 0.3s;
  pointer-events: none;
  max-width: 80%;
  text-align: center;
}

.toast.show {
  opacity: 1;
}

/* ===== Loading Mask ===== */
.loading-mask {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 2000;
  color: white;
  font-size: 14px;
  gap: 12px;
}

.loading-mask[hidden] {
  display: none;
}

.loading-spinner {
  width: 36px;
  height: 36px;
  border: 3px solid rgba(255, 255, 255, 0.3);
  border-top-color: white;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ===== Success Modal ===== */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 3000;
  padding: 20px;
}

.modal[hidden] {
  display: none;
}

.modal-content {
  background: white;
  border-radius: 12px;
  padding: 32px 24px 24px;
  text-align: center;
  max-width: 300px;
  width: 100%;
}

.modal-icon {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: #4cd964;
  color: white;
  font-size: 28px;
  line-height: 56px;
  margin: 0 auto 16px;
}

.modal-content h3 {
  font-size: 18px;
  margin-bottom: 8px;
}

.modal-content p {
  font-size: 14px;
  color: var(--color-text-secondary);
  margin-bottom: 24px;
}

.btn-modal-ok {
  width: 100%;
  height: 44px;
  background: var(--color-primary);
  color: white;
  font-size: 16px;
  border-radius: 6px;
  cursor: pointer;
}

/* ===== iOS WebView safe area ===== */
@supports (padding: env(safe-area-inset-bottom)) {
  .submit-area {
    padding-bottom: calc(20px + var(--safe-bottom));
  }
}

/* ===== Prevent zoom on iOS input focus ===== */
@media screen and (-webkit-min-device-pixel-ratio: 0) {
  select,
  textarea,
  input[type="text"],
  input[type="tel"],
  input[type="number"] {
    font-size: 16px;
  }
}

/* ===== Android WebView touch optimization ===== */
.form-input,
.form-select,
.btn-submit,
.btn-sms,
.upload-add {
  touch-action: manipulation;
}
